views:

81

answers:

6

Hey Guys,

I've got a function filling a HashMap(rMap) of String arrays. Once certain conditions are met e.g r.Map.size() != 0 I then, in another file (rMap is a Global variable) call the following String array[] = rMap.get(0) from this I attempt to System.out.println(array[0]) .

Thats the run of the program and I get a null pointer at System.out.println(array[0]);. Anyone have any ideas why this happens?

EDIT: I'm filling the map like so..

String center[] = new String[] { tname, tmessage, tlink, tsname };
Global.rMap.put(index, center);

Where all values in the array are variables that are strings. So the value I'm accessing it tname and It's not equal to null. I've checked. My Key value is a String

A: 

HashMap allows null values, so the value you are getting with String array[] =rMap.get(0); may be null. Accessing the null array then throws the NPE.

tob
+1  A: 

How are you filling the map? Are you certain that you're putting an non-null entry with an Integer key of 0 into it?

Well, we can be pretty certain that you aren't. Possible reasons:

  • An error in the filling code that results in the intended put not being executed, or with a different key value
  • You're using Short or Byte objects as keys
  • You're putting a null value into the map under the 0 key

You can answer this question for yourself by running the code in a debugger and looking at the contents of the map.

Update:

My Key value is a String

Well, that's your problem right there. rMap.get(0) will look for an Integer, and it will not match the entry for a String "0".

Michael Borgwardt
Updated my code to reflect your question.
Ulkmun
@Meowmix: Updated the answer to reflect your update.
Michael Borgwardt
+1  A: 

The array reference is null, most likely because no value (or a null) has been added to rMap, with key 0.

If possible, use generics to ensure that your keys are the correct type. You might also print out the values of the map prior to fetching array to see what is in the map. Stepping through the code with a debugger, with a watch on the rMap will also show what the map contains and when it is changed.

mdma
A: 

The get() method of Hashmap takes a key, not an index, to identify the value you want to get. If you want an ordered list of items, you'll need to use a List subclass (or enforce the ordering, yaourself). If you want to use a Hashmap, use the keys() method to get an enumeration of all the map's keys.

(removed above text, due to question clarification. leaving below text as, even though it's not the problem, it is an important consideration)

Also, you'll need to be very careful not to create race conditions, since you're working across threads. While Java's native Hashtable is synchronized, that doesn't mean the data in it is. That is, you can MyObj obj = get(xxx) in one htread, Nd start manipulating the obj in two separate threads, possibly stepping on each other. Depending upon your application, you may want to use Hashtable.remove() so that the data is gone from the map and cannot be re-used, or you may need some synchronized blocks (remove() may well be the simpler implementation, thoguh you'd have to gracefully handle conditions where the map first has data, then that data is gone).

atk
A: 

Try rMap.get(Integer.valueOf(0));

InsertNickHere
A: 

It's an issue when you invoke the get method of Map implementation, the signature of get method of java.util.Map is get(java.lang.Object),thus,any object will be accepted as its argument.

The key that works fine is either pass a string(value=0) as a key or overrides the hashCode and equals methods of argument object that is not a java.lang.String object.

Mercy