Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting a NullPointerException when adding data to a HashMap. I am writing a class to count the given frequencies of certain Objects. Here is my code (stripped of any unnecessary details):

public class FrequencyCounter {

    private Map<Object, Integer> freq;

    public FrequencyCounter() {
        freq = new HashMap<Object, Integer>();
    }

    public int add(Object key) {        
        System.out.println("Map is null: " + (freq == null));
        System.out.println("Key is null: " + (key == null));
        if (freq.containsKey(key)) {
            return freq.put(key, freq.get(key) + 1);
        }
        return freq.put(key, 1);
    }

    public static void main(String[] args) {
        FrequencyCounter fc = new FrequencyCounter();
        fc.add(new Object());
    }
}

The NPE is occuring on the line return freq.put(key, 1); Both println statements print false.

Do any of you know what I may be doing wrong?

share|improve this question
    
possible duplicate of Java: null pointer exception when unboxing Integer? –  Joe 15 hours ago
add comment

3 Answers

up vote 13 down vote accepted

This is because HashMap.put(key, value) will return the previous value associated with key, or null. In your code, you cannot return freq.put(key,1) as int because it is null.

share|improve this answer
    
I added a print statement to check if freq.put(key, 1) was returning null and it was. Thank you. –  ZR870 18 hours ago
1  
@ZR870 glad to help you:) –  locoyou 18 hours ago
    
I'm wonder, why there is no compile time error, in docs.oracle.com/javase/tutorial/java/data/autoboxing.html such condition is not provided –  atomAltera 17 hours ago
    
@atomAltera I think this can help you stackoverflow.com/questions/11897883/… –  locoyou 17 hours ago
    
@atomAltera The compiler cannot know It is null because freq.put(key, 1) will return a Integer object. –  locoyou 17 hours ago
show 1 more comment

add method returns int. int is primitive data type , which can not be null. freq.put(key,1)returns null so it throws exception. change return type of add method to Integer or add null check.

share|improve this answer
    
That's what I did, thanks. I just returned 0 because prior to this, the given object was present in the Map 0 times. –  ZR870 18 hours ago
add comment

since put() may return null so you must change return type to Integer which can be null:

public Integer add(Object key) {        
    System.out.println("Map is null: " + (freq == null));
    System.out.println("Key is null: " + (key == null));
    if (freq.containsKey(key)) {
        return freq.put(key, freq.get(key) + 1);
    }
    return freq.put(key, 1);
}

and must validate return value for null before using it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.