When boxed type java.lang.Boolean
is used as an expression it will throw NullPointerException
if the value is
null
as defined in Java Language Specification §5.1.8
Unboxing Conversion.
It is safer to avoid such conversion altogether and handle the null
value explicitly.
Boolean b = getBoolean(); if (b) { // Noncompliant, it will throw NPE when b == null foo(); } else { bar(); }
Boolean b = getBoolean(); if (Boolean.TRUE.equals(b)) { foo(); } else { bar(); // will be invoked for both b == false and b == null }