1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java NullPointerException in class using HashMap

  1. Apr 4, 2010 #1
    1.The problem statement, all variables and given/known data + 2. Relevant 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 (Text):

    // 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 (Text):

    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"));
     
    3. The attempt at a solution
    Code (Text):

    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!
     
  2. jcsd
  3. Apr 4, 2010 #2

    Filip Larsen

    User Avatar
    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: Apr 4, 2010
  4. Apr 5, 2010 #3
    Your advice was really helpful!
    Thank you so much!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java NullPointerException in class using HashMap
Loading...