org.sonar.l10n.java.rules.java.S2447.html Maven / Gradle / Ivy
Why is this an issue?
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
// ...
}
}
Resources
- CWE - CWE-476 - NULL Pointer Dereference
- CERT, EXP01-J. - Do not use a null in a case where an object is required
© 2015 - 2024 Weber Informatics LLC | Privacy Policy