1

Im trying to store data like associative array in JAVA. so i took list with maps

List<Map> processNeedImages = null; 
int flag =0;
if(c.moveToFirst()){
    do{
         Map<String, String> map = null;
         map.put("status", c.getString(c.getColumnIndex("system_url")));
         processNeedImages.add(flag++, map);
      }while(c.moveToNext());

I could not parse this data

List<Map> allImages = getData();
for (Map map:allImages){
    Log.d("listImage", String.valueOf(map.get("status")));
}

Even loop is iterating even once.

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71

2 Answers2

1

Map<String, String> map = null

should be replaced with

Map<String, String> map = new HashMap<>(); 

You are not creating a new map object. Rather you are adding the same map object everytime. although you are adding new values to the map for each cursor position but the object is only one. Hence your loop is going only one time.

Ankit Aggarwal
  • 5,317
  • 2
  • 30
  • 53
1

Initialize all variable in your code like below

List<Map> processNeedImages = new ArrayList<>(); 
int flag =0;
if(c.moveToFirst()){
    do{
         Map<String, String> map = new HashMap<>();
         map.put("status", c.getString(c.getColumnIndex("system_url")));
         processNeedImages.add(flag++, map);
      }while(c.moveToNext());
}

this will work fine.

You should read this answer too what is NullPointerException

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29