Callers of a Boolean method may be expecting to receive true or false in response. But Boolean
objects can take null as a possible value. Boolean methods should not return null unless the code is annotated
appropriately. With the proper annotation, the caller is aware that the returned value could be null.
Noncompliant code example
public Boolean isUsable() {
// ...
return null; // Noncompliant
}
public void caller() {
if (isUsable()) { // A NullPointerException might occur here
// ...
}
}
Compliant solution
@javax.annotation.Nullable
public Boolean isUsable() {
// ...
return null;
}
@javax.annotation.CheckForNull
public Boolean isUsable() {
// ...
return null;
}
public void caller() {
if (Boolean.True.equals(isUsable())) { // This caller knows to check and avoid ambiguity
// ...
}
}