Hello,
About your doubts:
I treat it in the bank, generating empty values for these records
It wouldn’t do that unless it makes sense in the business context of the application. Eventually it will make sense for one or the other field, but not for everyone. The data are those and your application needs to deal with them, so the problem will continue.
or I treat it in my code, like ignoring those exceptions?
You’ll deal with it in the code but do not ignore Exceptions. The less you get involved with exceptions, the better your code will look to me. What you need to do is map which fields can come null and prepare your application for this.
For example, if a column nome
can come null, instead of doing something like:
try {
nomeEmMaiusculo = nome.toUppercase();
} catch (NullPointerException ex) {
//ignorar, nome está null
}
Do:
if (nome != null) {
nomeEmMaiusculo = nome.toUppercase();
}
If you are using Java 8, you can improve this code using Optional
. So, when you receive the name that can be null, you immediately treat it as optional:
Optional<String> optNome = Optional.ofNullable(nome);
And to use the Optional
:
if (optNome.isPresent()) {
nomeEmMaiusculo = optNome.get().toUppercase();
}
So it’s pretty clear to the next code which name is optional and the concept of something null
, from this point on, it ceases to be a concern.
In the application because this is a programming error. Unless it should not have the null in the database, and in many cases should not even.
– Maniero