wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array of Hashmaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array of Hashmaps" Watch "Array of Hashmaps" New topic
Author

Array of Hashmaps

Nickolay Kolev
Greenhorn

Joined: Oct 17, 2002
Posts: 16
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

Joined: Jul 22, 2000
Posts: 9053
    
  12
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 ;


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Nickolay Kolev
Greenhorn

Joined: Oct 17, 2002
Posts: 16
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
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

Joined: Jul 22, 2000
Posts: 9053
    
  12

will avoid the NullPointerException.
Nickolay Kolev
Greenhorn

Joined: Oct 17, 2002
Posts: 16
Thanks everyone!
I get it now...
Declare AND INITIALISE, Declare AND INITIALISE, Declare AND INITIALISE... Written a 100 times across my screen.
nmk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of Hashmaps