Java

Java

Made by DeepSource

equals method does not handle null valued operands JAVA-S0110

Bug risk
Critical

This implementation of equals violates the contract defined by java.lang.Object.equals because it does not check for null being passed as the argument.

This can lead to the code throwing a NullPointerException when a null value is passed. This code violates the contract of equals because the receiver object (this) is always non null and so any null value passed is automatically not equal to this.

Examples

Problematic Code

@Override
public boolean equals(Object o) {
    return this.field == o.field;
}

// ...

MyClass a = new MyClass(3);

a.equals(null); // Throws NullPointerException.

Recommended

@Override
public boolean equals(Object o) {
    return o != null && this.field == o.field;
}

All equals methods should return false if passed a null value. Assuming that the operands are always non-null may easily allow NullPointerExceptions to occur.

References