Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of Hashmaps

 
Nickolay Kolev
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Is such a thing possible? It compiles OK, but I ger ArrayIndex out of bound for the following snippet:
HashMap[] result = new HashMap[5];

result[0].put("name", new String("The Name"));
What is wrong here?
Using Hashtables yields the same result.
Many thanks in advance,
nmk
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that you are trying to combine two different things into one line of code. Are you trying to:

1) populate a HashMap
2) put the HashMap that you populated into the array of HashMaps
??

HashMap[] result = new HashMap[5];
HashMap myHashMap = new HashMap();
myHashMap.put("name", new String("The Name"));
result[0] = myHashMap ;
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We will need some more code to see what the problem is. You should get a NullPointerException not an ArrayIndexOutOfBoundsException based on that snippet of code.
 
Nickolay Kolev
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was my bailout strategy. I wanted to do it shorter. I still don't get why it doesn't work.
result[0] is a legal address of the first Hashmap, right?
Sow why can't I "put" anything there?
Thanks anyway!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, we need to see more code. There must have been some other lines between
HashMap[] result = new HashMap[5];
and
result[0].put("name", new String("The Name"));
or you would have gotten a NullPointerException. You need to show us the actual code you're using here, in order to get an explanation.
Or if this really is the code you're looking at, then delete your .class file and recompile. If that doesn't work, study your class path carefully and look for a .class or .jar file in another directory which is getting run instead of the class you're trying to execute. One way or another, you're not running the code you show above.
[ February 16, 2003: Message edited by: Jim Yingst ]
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
result[0] is a legal address of the first Hashmap, right?

result[0] is not a legal address until you initialize it. You've declared it, but you have not initialized it. Therefore, you should be getting a NullPointerException, as Jim and Thomas have already pointed out.

I see nothing here that would cause an ArrayIndexOutOfBoundsException.
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will avoid the NullPointerException.
 
Nickolay Kolev
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!
I get it now...
Declare AND INITIALISE, Declare AND INITIALISE, Declare AND INITIALISE... Written a 100 times across my screen.
nmk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic