This is a problem that developers without much experience often face: they either do not know or do not trust the contract they are participating in and defensive end up exaggerating in checking by null's. Additionally, when they write their own code, they have the habit of returning null's to indicate something, and this ends up generating the need to check for null's by the method that tried to invoke the object.
In summary, there are two cases that require verification by null's:
1) When null
is not a valid contract response:
In this case, use the AssertionError feature or just leave the exception occurs, use the information generated by error
or exception
to debug the code and understand why the unwanted condition happened, then fix your logic to avoid this condition. Make all sorts of tests possible, the sooner the error or exception occurs the better, you will not want to discover a bug once you've launched your program.
assertion
is a feature that has been added to Java 1.4 but is still not as widely practiced by programmers. It's great to find bugs because you can do tests to make sure you're getting the expected result at certain points in the program.
"- But I can do these tests using if
s, correct?"
Correct, however with assertions
you test in a much more practical way as you do not need to write a whole code to manipulate and display the error message. Furthermore, assert
is not enabled by default, so you can enable it while debugging your program, and when you distribute it, the assert
s is normally disabled. Using a logic with if
you should remember to erase the debugging code before distributing your program so that you do not run the risk of displaying a message to the user that should never have been displayed.
Syntax:
assert <condição>
or
assert <condição> : <objeto>
If the condition fails a AssertionError
is thrown. If an object has been indicated in the second parameter the toString()
method of objeto
will be added to the error information posted by AssertionError
.
Example:
public Pessoa obterPessoa(int id) {
Pessoa resultado = null;
if (id > 50) {
resultado = datasource.getPessoa(id);
} else {
resultado = new Pessoa(id);
}
assert resultado != null : "pessoa nula no método obterPessoa()";
return resultado;
}
In summary:
Know your contract, debug it and trust it.
2) If null
is a valid contract response:
In this case there is not much choice, you have to test the object before using it.
Or if you want to improve your code, you can change it and initialize its objects and their attributes, so that they never return null
, nor that the object method returns empty this already avoids the dreaded NullPointerException
.
You may want to take a look at the Null Object Pattern
Example:
public interface Animal {
public void makeSound();
}
public class Dog implements Animal {
public void makeSound() {
System.out.println("woof!");
}
}
public class NullAnimal implements Animal {
public void makeSound() {
}
}
Instead of initializing an object of class Animal as null
initialize it as NullAnimal
Source (s): Avoiding "! = null" statements in Java?