Equals Called With Null

ID

java.equals_null_arg

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:570, reliability

Description

Reports calls of the form x.equals(null).

Rationale

The Object.equals contract explicitly states that x.equals(null) must always return false. Writing the comparison that way is therefore either a typo ("I meant `x == null`") or a misunderstanding of the contract.

Writing x == null directly is clearer and avoids both the virtual dispatch and the risk that x itself is null (which would turn the expression into an NPE).

// Bad — always false, and NPE if x is null
if (x.equals(null)) { ... }

// Good
if (x == null) { ... }

Remediation

Replace x.equals(null) with x == null.