I was trying to compare the name of two different Objects, but I kept getting exceptions when using the equals()
method to compare an item to null. I've tried so many ways, including other.equals(haha), haha.equals(other), etc, but all failed.
public final class ItemImpl implements Item {
private final String name;
public ItemImpl(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null!");
}
this.name = name;
}
@Override
public String getName() {
return this.name;
}
public boolean equals(Object other) {
Object haha = name;
return other.toString().equals(haha.toString());
}
public String toString() {
return this.name;
}
}