Results 1 to 3 of 3
  1. #1
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default HashMap assignment and null pointer exception

    Here is some simple piece of code:
    Java Code:
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class InvertedIndex implements Serializable {
    
    	private HashMap<String, ArrayList<Integer>> index;
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @param token
    	 * @param id
    	 */
    	public void add(String token, int id) {
    		ArrayList<Integer> al = new ArrayList<Integer>();
    		al.add(id);
    		System.out.println(al.toString());
    		System.out.println(token);
    		index.put(token, al);
    	}
    	
    	public void print(){
    		System.out.println(index.toString());
    	}
    
    	/**
    	 * @param args
    	 * @throws IOException
    	 */
    	public static void main(String[] args) {
    		String[] words = new String[4];
    		words[0] = "one";
    		words[1] = "two";
    		words[2] = "other";
    		words[3] = "two";
    		InvertedIndex ii = new InvertedIndex();
    		for(int i = 0; i< words.length; i++){
    			ii.add(words[i],i);
    		}
    	}
    
    }
    When I execute it, I get a null pointer exception (which is by far the least informative exception I've seen in Java :p ) and I can't understand why. The exception is triggered when the index.put(token, al); is executed.

    Can you please guide me?

  2. #2
    merik is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Well, I found the problem .. my HashMap was not initialized. But I wish there was a day when Java would return "object not initialized" exception instead of "null pointer exception".

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Ah, but a NullPointerException doesn't necessarily arise out of forgetting to initialize a reference. A method can return null, and a NPE will be thrown anywhere you attempt to dereference the returned value.

    db

Similar Threads

  1. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  2. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  3. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 PM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •