Question

I have an equals method in a Position class. The class contains two instance variables, row and col, that represent the location in a grid. I need to be able to check if two different positions are equal, using an equals method. However, with my current equals method I get an "Int cannot be dereferenced" error upon compiling. I cannot find how to fix this, and the instructor has specified that there should be no reason to overwrite the hashcode. My equals method is below, any help is appreciated.

@Override
    public boolean equals(Object other) {
        if (null == other) return false;
        if (this == other) return true;
        if (!(other instanceof Position)) return false;
        Position that = (Position) other;
        return row.equals(that.getRow()) && col.equals(getCol());
    }

EDIT: row and col are of type int.

Était-ce utile?

La solution

If row and/or column is int, then it doesn't have any method at all since it is a primitive type. Change it to Integer or use == instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top