Java

Java

Made by DeepSource

equals method does not handle null valued operands JAVA-E0110

Bug risk
Critical

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

equals must always return false if its argument is null.

This can lead to the code throwing a NullPointerException when a null value is passed. One property of any non-static method in Java is that the receiver object (this) is always non-null. This code violates the contract of equals because any null value passed is automatically not equal to this.

Bad Practice

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

// ...

MyClass a = new MyClass(3);

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

Recommended

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

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

References