ArrayList<HashMap<String,String>> get values

4k views Asked by At

Help getting values from ArrayList

data = new ArrayList<Map<String, String>>();
for (int i = 0; i < marks.length; i++) {
    HashMap<String, String> m = new HashMap<String, String>();
    m.put("title", marks[i].getUserName());
    m.put("mark", marks[i].getMark().toString());
    m.put("type", marks[i].getType().toString());
    m.put("description", marks[i].getDescription());
    data.add(m);
}

Trying to get out of the adapter an ArrayList and write the values ​​to an array:

data = simpleAdapter.getData();
MarksRead[] marksReads = new MarksRead[data.size()];
for (int i = 0; i < data.size(); i++) {
    marksReads[i].setDescription(data(i).get("description"));
    marksReads[i].setMark(Integer.valueOf(data.get(i).get("mark")));
    marksReads[i].setType(Integer.valueOf(data.get(i).get("type")));
}

On line marksReads[i].setDescription(data(i).get("description")); output is Exception NullPointerException.

Help, how to do it right.

1

There are 1 answers

0
Blackbelt On BEST ANSWER

you have to create the object before you can use it

for (int i = 0; i < data.size(); i++) {
    marksReads[i] = new MarksReads();
    marksReads[i].setDescription(data(i).get("description"));
    marksReads[i].setMark(Integer.valueOf(data.get(i).get("mark")));
    marksReads[i].setType(Integer.valueOf(data.get(i).get("type")));
 }