Why is this an issue?

When boxed type java.lang.Boolean is used as an expression to determine the control flow (as described in Java Language Specification §4.2.5 The boolean Type and boolean Values) it will throw a 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.

Note, however, that no issues will be raised for Booleans that have already been null-checked or are marked @NonNull/@NotNull.

Noncompliant code example

Boolean b = getBoolean();
if (b) {  // Noncompliant, it will throw NPE when b == null
  foo();
} else {
  bar();
}

Compliant solution

Boolean b = getBoolean();
if (Boolean.TRUE.equals(b)) {
  foo();
} else {
  bar();  // will be invoked for both b == false and b == null
}


Boolean b = getBoolean();
if(b != null){
  String test = b ? "test" : "";
}

Exceptions

The issue is not raised if the expression is annotated @NonNull / @NotNull. This is useful if a boxed type is an instantiation of a generic type parameter and cannot be avoided.

List<Boolean> list = new ArrayList<>();
list.add(true);
list.add(false);
list.forEach((@NonNull Boolean value) -> {
  // Compliant
  if(value) {
    System.out.println("yes");
  }
});

@NonNull Boolean someMethod() { /* ... */ }

// Compliant
if(someMethod()) { /* ... */ }

@NonNull Boolean boxedNonNull = Boolean.TRUE;

// Compliant
if(boxedNonNull) { /* ... */ }

Resources