-
HashMap Null error
Hi. I have a HashMap which maps a String to a JLabel:
Code:
tileSlots.put("slot0", slot0);
But later on in the codes when I try to get the JLable via the HashMap, it gives me the "NullPointerException" exception error.
Error at:
Code:
JLabel x = tileSlots.get("slot"+i);
print(x.getName());
Any idea wats wrong?
P.S:- tileSlots is declared as public and the print line is where the error points to. The code where the error occurs is in a loop, thats why am using "i" variable.
-
Re: HashMap Null error
The code looks fine. Perhaps you are using two different Maps (one local Map with the same name :D)?
-
Re: HashMap Null error
I just checked and am not using a local map. The "tileSlots" var is in green as am using Netbeans as SDK. Would you like the whole IF statement if it might help? (thnx for the quick reply though :)-: )
-
Re: HashMap Null error
Yes, maybe a little bit more code would help.
-
Re: HashMap Null error
There:
Code:
if (data.contains("Tile") && data.contains("L")) {
dominogameServer.Tile[] tileSet = (dominogameServer.Tile[]) input;
for(int i=0;i<tileSet.length;i++){
tiles.add(new Tile(tileSet[i].getTileNum())); // 'tiles' type is 'ArrayList'
JLabel x = tileSlots.get("slot"+i);
print(x.getName());
showIcon(x, tileSet[i].getTileNum());
}
for (int i=0;i<tileSet.length;i++){
print("tile: " + tileSet[i].getTileNum());
}
}