Boolean Method Returns Null

ID

java.boolean_return_null

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:476, reliability

Description

Reports methods with return type Boolean (boxed) that explicitly return null. Returning null from a Boolean-typed method causes a NullPointerException when the caller auto-unboxes the result.

Rationale

Callers of a Boolean-typed method typically write if (isValid()) which auto-unboxes the result. If the method returns null, this triggers an unexpected NullPointerException. The three-valued logic (true / false / null) is rarely intentional and always error-prone.

// Bad - auto-unboxing will throw NPE
public Boolean isReady() {
    return null;
}

if (isReady()) { /* NPE here */ }

Remediation

Return Boolean.TRUE or Boolean.FALSE instead of null. If a tri-state is genuinely needed, use Optional<Boolean> or a dedicated enum.

// Good
public Boolean isReady() {
    return Boolean.FALSE;
}