-5

I have a problem about getting a value from my ArrayList<HashMap<String, String>>.

My code is:

ArrayList<HashMap<String, String>> myArrayList;

and then:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
myArrayList.add(map);

If I want to get the name, for example, I tried as follow but I obtain a runtime error (the application crashes):

System.out.println(myArrayList.get(1).get(TAG_NAME));

How can I solve it?

Thank you very much!

2
  • 1
    Never forget to include the complete error messages you get. It contains the necessary information to fix the error... Commented May 14, 2013 at 13:52
  • Do you initialise myArrayList anywhere? myArrayList = new ArrayList<HashMap<String, String>>(); Commented May 14, 2013 at 14:33

2 Answers 2

6

System.out.println(myArrayList.get(1).get(TAG_NAME));

ArrayList is 0-based. get(0) instead.

Sign up to request clarification or add additional context in comments.

4 Comments

I've already tried as you say, but the array list is populated by 64 hash map and the problem persists
@kareth perhaps post your error message along with some more information, or really it is hard to tell.
what do you mean by 64 has map ? size of the arraylist ?
Exactly, I'm sorry for my unclear problem description. I fill my ArrayList with (key,value), and its size is 64. The problem now is to take a field (for example "name") from my HashMap inside ArrayList. If I want to take a String (eg the "name" of the 4th HashMap inside the ArrayList) how can I do?
0

You can loop through the list like this

   for(Map<String, String> map : myArrayList)
    {
        String tagName = map.get(TAG_NAME);
        System.out.println(tagNAme);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.