-2

I am getting NullPointerException in the last statement.Please help.

ArrayList<HashMap<String, String>> contactList;
for(int i=0; i < jsonArray.length(); i++){
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    HashMap<String,String> contact = new HashMap<String,String>();
    contact.put(TAG_ID, jsonObject.optString("id").toString());
    contact.put(TAG_NAME,jsonObject.optString("name").toString());
    contact.put(TAG_EXT,jsonObject.optString("extension").toString());
    contactList.add(contact);
}
Vivek Singh
  • 2,047
  • 11
  • 24
hushan
  • 63
  • 5
  • 6
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Dhaval Patel Jan 22 '16 at 06:39

4 Answers4

1

you have not initialize contactList.

 ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
nikk
  • 407
  • 3
  • 7
0

you have not initialize contactList, it should be initialized before use

ArrayList<HashMap<String, String>> contactList=new ArrayList<HashMap<String, String>>();
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

ArrayList should be iitialized first. Try this,

ArrayList<HashMap<String, String>> contactList;
 ArrayList<HashMap<String, String>> contactList=new ArrayList<HashMap<String, String>>();
 for(int i=0; i < jsonArray.length(); i++){
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 HashMap<String,String> contact = new HashMap<String,String>();
 contact.put(TAG_ID, jsonObject.optString("id").toString());
 contact.put(TAG_NAME,jsonObject.optString("name").toString());
 contact.put(TAG_EXT,jsonObject.optString("extension").toString());
 contactList.add(contact);
}
USER9561
  • 1,084
  • 3
  • 15
  • 41
0

You have to initialize contactList `

ArrayList<HashMap<String, String>> contactList=new ArrayList<HashMap<String, String>>();
 for(int i=0; i < jsonArray.length(); i++){
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 HashMap<String,String> contact = new HashMap<String,String>();
 contact.put(TAG_ID, jsonObject.optString("id").toString());
 contact.put(TAG_NAME,jsonObject.optString("name").toString());
 contact.put(TAG_EXT,jsonObject.optString("extension").toString());
 contactList.add(contact);
}`
Raju
  • 448
  • 1
  • 9
  • 24