13

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?

zr870
  • 1,075
  • 1
  • 8
  • 9
  • possible duplicate of [Java: null pointer exception when unboxing Integer?](http://stackoverflow.com/questions/1811706/java-null-pointer-exception-when-unboxing-integer) – Joe Mar 26 '14 at 07:50

3 Answers3

17

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.

locoyou
  • 1,697
  • 1
  • 15
  • 19
  • I added a print statement to check if `freq.put(key, 1)` was returning null and it was. Thank you. – zr870 Mar 26 '14 at 04:25
  • I'm wonder, why there is no compile time error, in http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html such condition is not provided – atomAltera Mar 26 '14 at 04:54
  • @atomAltera I think this can help you http://stackoverflow.com/questions/11897883/why-does-the-java-compiler-sometimes-allow-the-unboxing-of-null – locoyou Mar 26 '14 at 05:05
  • @atomAltera The compiler cannot know It is null because `freq.put(key, 1)` will return a Integer object. – locoyou Mar 26 '14 at 05:10
  • @locoyou Yes, but this Integer object (more precisely - pointer to Integer object) could point to nothing. But I think java developers doing this right, I mean implicit unboxing – atomAltera Mar 26 '14 at 05:44
2

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.

Sagar Gandhi
  • 925
  • 6
  • 20
  • 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 Mar 26 '14 at 04:26
1

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.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110