# Java NullPointerException in class using HashMap

• Comp Sci
1.Homework Statement +

## Homework Equations

Hello,
I really need your help, because I'm a Java newbie and I've got stuck in this Java homework.
I should implement class ObjectCounter, which has to include two methods:

Code:
// increase the counter for given object
// returns the current number of objects, i.e. current value of counter
public int getCount(Object o)
Implemented class ObjectCounter has to use the HashMap and all of it has to be called this way:

Code:
ObjectCounter objectCounter = new ObjectCounter();
// has to print 1
System.out.println(objectCounter.getCount("hi"));
// has to print 0
System.out.println(objectCounter.getCount("hello"));
// has to print 2
System.out.println(objectCounter.getCount("hi"));
// has to print 1
System.out.println(objectCounter.getCount("hello"));

## The Attempt at a Solution

Code:
public class ObjectCounter {

private HashMap<Integer, Integer> map = new HashMap();

int counter = getCount(o);
if (counter == 0) {
map.put(o.hashCode(), 1);
} else {
map.remove(o.hashCode());
map.put(o.hashCode(), counter++);
}
}

public int getCount(Object o) {
System.out.println(map.get(o.hashCode()));
return map.get(o.hashCode());
}
}
But it always ends with java.lang.NullPointerException, but I don't know how to repair it.

Thank you very much!

Related Engineering and Comp Sci Homework Help News on Phys.org
Filip Larsen
Gold Member
The NullPointerException (NPE) comes from the automatic unboxing your getCount method performes. Since this method has been declared to return an int (a value object) any Integer you get from your map will (automatically by the compiler) be converted to an integer. However, when the Integer is null (like when the key is not found in the map) this conversion will throw NPE. You can fix this by adding a test in your getCount method so that when the key is not found in the map you return zero.

Let me also make the following comments:

You should most likely not use the hash code of objects as key, since that means two unrelated objects which by coincidence have the same hash code would be counted as the same object. Use the objects themselves as keys instead (you then also need to change the Map generic parameter for the key from Integer to Object). Also your HashMap instantiation should include templates, and it is considered good style to use the interface of a collection class instead of the implementation class. In total you then get the declaration "private Map<Object,Integer> map = new HashMap<Object,Integer>();"

You do not have to remove a key from your map before you update its value. You can just put the new key and value, and the old value will be removed automatically (in fact, put returns the old value if you need it).

When you have fixed your getCount method to properly return zero for objects not in the map, you do not have to test for zero in the add method. The body of the add method can be implemented in a single line, if you want to.

Last edited: