A boxed boolean value (java.lang.Boolean
) is being used in a potentially dangerous manner. Such usage may lead to a NullPointerException
being thrown.
Java's primitive types differ from normal classes in that primitives can never be null. However, classes can be null, even primitive wrapper classes such as Boolean
or Character
. For Boolean
in particular, this means that turning a boolean
into a Boolean
promotes it from a binary true
or false
to a ternary true
, false
or null
. This may be undesirable in most cases, and is worth avoiding.
The nullability of wrapper types becomes an issue when the wrapper types are used directly in expressions without null checks.
In the example below, the if
statement evaluates the value of nullable
when its value is null
. This will lead to a NullPointerException
being thrown.
Boolean nullable = null
if (nullable) { // This would throw a NullPointerException.
// ...
}
You could perform an explicit comparison with the required value.
if (nullable == true) {
// ...
}
// OR
if (Boolean.TRUE.equals(nullable)) {
// ...
}
Using Boolean.TRUE
here can prevent an unnecessary unboxing conversion when performing a comparison.