Hello,
About your questions:
I treat this in the database, generating empty values for these records
I would not do this, unless it makes sense in the business context of the application. Eventually it will make sense for one or another field, but not for everyone. The data is the ones and your application needs to deal with them, so the problem will continue.
Or do I deal with this in my code, kind of ignoring these exceptions?
You'll deal with the code but do not ignore Exceptions . The less involved with exceptions, the better your code will look. What you need to do is map which fields can be null and prepare your application for this.
For example, if a nome
column can be null, instead of doing something like:
try {
nomeEmMaiusculo = nome.toUppercase();
} catch (NullPointerException ex) {
//ignorar, nome está null
}
Make:
if (nome != null) {
nomeEmMaiusculo = nome.toUppercase();
}
If you are using Java 8, you can improve this code by using Optional
. So when you get the name that can be null, you immediately treat it as optional:
Optional<String> optNome = Optional.ofNullable(nome);
And to use Optional
:
if (optNome.isPresent()) {
nomeEmMaiusculo = optNome.get().toUppercase();
}
So it is very clear to the following code that name is optional and the concept of something null
, from this point on, is no longer a concern.