Results 1 to 3 of 3
- 03-10-2011, 08:41 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
HashMap assignment and null pointer exception
Here is some simple piece of code:
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.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); } } }
Can you please guide me?
- 03-10-2011, 08:44 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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".
- 03-11-2011, 05:18 AM #3
Similar Threads
-
null pointer exception
By marvelk in forum Advanced JavaReplies: 8Last Post: 02-01-2011, 09:02 AM -
Null pointer exception
By talha06 in forum JDBCReplies: 5Last Post: 07-14-2009, 01:12 AM -
null pointer exception
By jyothi.priyanka in forum New To JavaReplies: 12Last Post: 03-11-2009, 05:04 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks