user3353751
user3353751

Reputation: 25

Think I may be misunderstanding the Map class methods

This keeps giving a null pointer exception... I feel like I'm missing something. This is a HashMap<Character, Integer>, by the way. Maybe it's a wrapper/primitive thing? I don't know.

int amount = 0;
    for (int i = 0; i < aMap.size(); i++){
        if (!aMap.get(i).equals(null))
            if (aMap.get(i).equals(c))
                amount++;
    }

Upvotes: 0

Views: 42

Answers (1)

Njol
Njol

Reputation: 3279

aMap.get(i).equals(null) throws a NPE if aMap.get(i) is null. You have to use == to test for reference equality when testing for null:

if (aMap.get(i) != null)

Upvotes: 2

Related Questions