I am trying to get a Point2D.Double out of a HashMap for a java game.
public void Undo(){
if(Moves.size() >=1)
{
GW.ClearBoard();
Ships.clear();
Move m =Moves.remove(0);
HashMap<Ship,Point2D.Double> shipMoves = m.getShips();
for(Ship s: shipMoves.keySet()){
if(shipMoves.get(s)!= null){
Point2D.Double pos = shipMoves.get(s);
String type = s.getType();
GW.setShipLocation(pos, type);
s.setPosition(pos.x, pos.y);
Ships.add(s);
}
}
System.out.println("Undo");
}
}
As you can see from the screen captures of me debugging in eclipse, the hash map contains an entry for key=MasterShip(id=87) with a value containing Point2D.Double and the input S is a ship with id=87 and yet the program still returns null and I have no idea why or what I can do about this. I have checked the values in the Move class through the debugger as well.
Correction, there would be images but apparently I cannot post images yet.
edit
You can also iterate over the entrySet() of the map. – Nick Hristov
Thank you, the entryset() method of solving this issue worked perfectly, dunno how to up-vote or set a comment as an answer though.