I have the following method in one of my classes. It's just a public wrapper around a HashMap (named teamOfPlayer, with keys of Player objects and values of Integer objects), nothing more.
public int getTeamOfPlayer(Player p)
{
return teamOfPlayer.get(p);
}
As long as my Player objects inherited the default hashCode() method from Object, this worked just fine. However, in order to save my Player objects to files, I implemented my own hashCode(). Suddenly, the method began throwing a NullPointerException.
I expanded the method as follows to print out some debugging information, but it's left me even more confused than before.
public int getTeamOfPlayer(Player p)
{
Object[] o = teamOfPlayer.keySet().toArray();
Player p2 = (Player) o[0];
System.out.println("getTeamOfPlayer(" + p + ")"
+ "\n\thash of argument is " + p.hashCode()
+ "\n\tkeySet() of hashmap is " + teamOfPlayer.keySet()
+ "\n\tcontainsKey() of hashmap is " + teamOfPlayer.containsKey(p)
+ "\n\tplayer extracted from keySet() is " + p2
+ "\n\tplayer extracted from keySet() has hash of" + p2.hashCode()
+ "\n\targument.equals(key) returns " + p.equals(p2)
+ "\n\tkey.equals(argument) returns " + p2.equals(p));
int i = teamOfPlayer.get(p);
return i;
}
The output of the above method is here:
getTeamOfPlayer(main.data.entities.Player@89f632df)
hash of argument is -1980353825
keySet() of hashmap is [main.data.entities.Player@89f632df]
containsKey() of hashmap is false
player extracted from keySet() is main.data.entities.Player@89f632df
player extracted from keySet() has hash of-1980353825
argument.equals(key) returns true
key.equals(argument) returns true
The exception is thrown on the "int i = teamOfPlayer.get(p);" line, meaning that the map is returning null (because it doesn't think it contains the key). I know that's why the exception is being thrown. However, I think I've proven that the key DOES exist in the map. What's going on?
--
Update: Here are the equals() and hashCode() methods.
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
Player player;
if (obj != null && obj instanceof Player)
player = (Player) obj;
else
return false;
if (status != player.status || !name.equals(player.name) || race != player.race || weeksOut != player.weeksOut || injuryType != player.injuryType
|| XP != player.XP)
return false;
for (int i = 0; i < 8; i++)
{
if (attributes[i] != player.attributes[i])
return false;
if (injuries[i] != player.injuries[i])
return false;
}
for (int i = 0; i < 28; i++)
{
if (hasSkill[i] != player.hasSkill[i])
return false;
}
for (int i = 0; i < 4; i++)
{
if (equipment[i] != player.equipment[i])
return false;
}
return true;
}
@Override
public int hashCode()
{
int hash = 11;
hash = 31 * hash + status;
hash = 31 * hash + name.hashCode();
hash = 31 * hash + race;
hash = 31 * hash + weeksOut;
hash = 31 * hash + injuryType;
hash = 31 * hash + XP;
for (int i = 0; i < 8; i++)
{
hash = 31 * hash + attributes[i];
hash = 31 * hash + injuries[i];
}
for (int i = 0; i < 28; i++)
hash = hash + (hasSkill[i] ? 1 : 0);
for (int i = 0; i < 4; i++)
hash = 31 * hash + equipment[i];
return hash;
}
equalsto be consistent withhashCode? - rgettman