[ACCEPTED]-Java: Clean way of avoiding NullPointerException in equals checks-coding-style

Accepted answer
Score: 18

You can use a helper method like

public static boolean isEqual(Object o1, Object o2) {
    return o1 == o2 || (o1 != null && o1.equals(o2));


Score: 10

Google Guava provides Objects.equal(Object, Object) which checks for equality while 4 taking into consideration that either of 3 the parameters might be null:

return Objects.equal(this.getStreet(), other.getStreet())
    && Objects.equal(this.getStreetNumber(), other.getStreetNumber())
    && Objects.equal(this.getStreetLetter(), other.getStreetLetter())
    && Objects.equal(this.getTown(), other.getTown());

It's also worth 2 pointing out that Objects has other helper 1 methods for implementing hashCode() and toString().

Score: 9

You could do the following:

public boolean equals(Object obj) 
    if (this == obj) {
        return true;

    if (obj == null) {
        return false;

    if (getClass() != obj.getClass()) {
        return false;

    Address other = (Address) obj;

    return equals(this.getStreet(),other.getStreet())
        && equals(this.getStreetNumber(), other.getStreetNumber())
        && equals(this.getStreetLetter(), other.getStreetLetter())
        && equals(this.getTown(), other.getTown());

private boolean equals(Object control, Object test) {
    if(null == control) {
        return null == test;
    return control.equals(test);

Java 7 introduced 2 built-in support for this use case with 1 the java.util.Objects class see:

Score: 4

I have a helper class Checker w/ a static 1 method:

 public static boolean isEquals(final Object o1, final Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);

so, in the equals method,

 return Checker.isEquals(this.getStreet(), other.getStreet())
        && Checker.isEquals(this.getStreetNumber(), other.getStreetNumber())
        && Checker.isEquals(this.getStreetLetter(), other.getStreetLetter())
        && Checker.isEquals(this.getTown(), other.getTown());
Score: 2

There is no really clean way to do that; the 4 best option is probably to have your IDE 3 generate the code for you. Eclipse can do 2 it via the Source -> Generate hashCode() and 1 equals() context menu.

Score: 2

You can use Objects.equal from Googles guava or the EqualsBuilder from 1 apache commons

Score: 1

You can use java.util.Objects.equals(a, b) if you want an official way (built-in).

(There is already a mention to it in one of the answers, but more as a sidenote)




Objects.equals(this.getStreet(), other.getStreet())


Score: 0

I'd consider defining some of the equals 6 methods as static class methods, like say 5 for the Street objects. This way you don't 4 ever attempt to call the .equals() method 3 on a null.

A sample function might look like:

public static boolean equals(Object one, Object two)

Also, it's 2 good practice to put checks like

if (obj == null)
   return false;

at the very 1 beginning of a function.

Score: 0

Apache Commons Lang provides the EqualsBuilder helper class for equality 2 comparissons. There is also one for hash 1 codes.

return new EqualsBuilder()
.append(this.getStreet(), other.getStreet())
.append(this.getStreetNumber(), other.getStreetNumber()
.append(this.getStreetLetter(), other.getStreetLetter())
.append(this.getTown(), other.getTown())).isEquals();

More Related questions