I suspect the problem isn't spamScore
being null
- it's the value in the map being null, because you haven't populated the map. You're then trying to unbox that null
reference to an int
, and that fails. So think of your code like this:
Integer startingScoreReference = spamScore.get(name);
int startingScore = startingScoreReference.intValue();
Now obviously if startingScoreReference
is null
(which it will be if there's no entry for name
in your map) then the second line will cause a NullPointerException
.
So basically you need to either ensure that your map will always contain an entry for name
, or you need to handle the case where there's no such entry. For example, you might want:
Integer mapValue = spamScore.get(name);
int startingScore = mapValue == null ? 0 : mapValue.intValue();
(The intValue()
call could be a cast to int
instead, depending on your preference.)