Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HashMap get comes back with null even tho key is in it

I’m trying to create a swing chessboard with icons and i have trouble with putting the icons onto the JButtons using a HashMap.
Here are the classes that i’m working with:

Main Class

public class GameGUI  extends JFrame {
    private JButton tiles[][] = new JButton[8][8];
    private HashMap<PieceKey, ImageIcon> icons = new HashMap<>();

    public GameGUI(){
        //swing shenannigans

        initImages();

        Tile[][] fenTiles = game.getFen().getTiles();
        for(int row = 0; row <= 7; row++){
            for(int column = 0; column <= 7; column++){
                Piece piece = fenTiles[row][column].getPiece();
                if(piece != null) {
                    tiles[row][column].setIcon(icons.get(new PieceKey(piece.getType(), piece.getColor())));
                }
            }
        }


    }

    public void initImages(){
        icons.put(new PieceKey(PieceType.pawn, Team.white), new ImageIcon("pieces/wpawn.png"));
        //.....
        }

    public static void main(String args[]){
        GameGUI asd = new GameGUI();
    }
}

PieceKey class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class PieceKey {
    PieceType type;  //enum
    Team color;      //enum

    public PieceKey(PieceType type, Team color) {
        this.color = color;
        this.type = type;
    }
    
    @Override
    public boolean equals(Object o){
        if(this == o)
            return true;
        if(!(o instanceof PieceType))
            return false;
        PieceKey key = (PieceKey) o;
        return this.type == key.type && this.color == key.color;
    }

    @Override
    public int hashCode(){
        return type.toString().hashCode() * color.toString().length();
    }
}

Team enum

public enum Team {
    white, black;
}

PieceType enum

public enum PieceType {
    pawn, rook, knight, bishop, king, queen;
}

My problem is that whenever i call

icons.get(new PieceKey(piece.getType(), piece.getColor()));

It return null, so i cant put the icons onto the buttons, it works fine if i do it manually so the problem is with the HashMap. I tried to override the equals and the hashcode function in the PieceKey class but it doesn’t seem to work.

>Solution :

The problem may be in your PieceKey equals method. You are using incorrectly PieceType while using instanceof:

@Override
public boolean equals(Object o){
    if(this == o)
        return true;
    // Please, note this, it will always return false, and the `Map`
    // `equals` method for `get` and `put` will not work
    if(!(o instanceof PieceType))
        return false;
     PieceKey key = (PieceKey) o;
     return this.type == key.type && this.color == key.color;
}

If should be:

@Override
public boolean equals(Object o){
    if(this == o)
        return true;
    // Please, note this
    if(!(o instanceof PieceKey))
        return false;
     PieceKey key = (PieceKey) o;
     return this.type == key.type && this.color == key.color;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading