equals
does not check for null
values42 }
43
44 @Override
45 public boolean equals(Object o) { // JAVA-E011046 return this.hashCode() == o.hashCode();47 }48}
Description
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 NullPointerException
s to occur.
References
- Spotbugs - NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT