• Support PF! Buy your school textbooks, materials and every day products Here!

Java NullPointerException in class using HashMap

  • Comp Sci
  • Thread starter Kat3rina
  • Start date
  • #1
4
0
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
public void add(Object o)
// 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(); 
objectCounter.add("hi"); 
// has to print 1
System.out.println(objectCounter.getCount("hi"));
// has to print 0
System.out.println(objectCounter.getCount("hello")); 
objectCounter.add("hi");
// has to print 2
System.out.println(objectCounter.getCount("hi")); 
objectCounter.add("hello"); 
// 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();

    public void add(Object o) {
        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!
 

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,237
169
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:
  • #3
4
0
Your advice was really helpful!
Thank you so much!
 

Related Threads for: Java NullPointerException in class using HashMap

Replies
4
Views
2K
Replies
2
Views
718
Replies
0
Views
1K
  • Last Post
Replies
1
Views
2K
Replies
5
Views
3K
Replies
47
Views
5K
  • Last Post
Replies
3
Views
4K
  • Last Post
Replies
11
Views
2K
Top