begeegs begeegs - 10 months ago 64
JSON Question

HashMap and JSON lookup errors (Null reference)

I am having a problem with a bit of code which does a json looking and is returning a 'null error' which I don't know why. I can see the values via a log, so I know that the information is in there. When the HashMap is being created, it is bombing out.

Can someone please let me know where I am going wrong?

HashMap<String, List<String>> listDataChild;
private List<String> subList = new ArrayList<String>();
private List<String> subTitle = new ArrayList<String>();

for (int i = 0; i < response.length(); i++) {

JSONObject obj = response.getJSONObject(i);
String x = obj.getString("category");
String y = obj.getString("title");


listDataChild.put(subList.get(i), subTitle);

/* Log.e("subList", subList.get(i).toString());
Log.e("subTitle", subTitle.get(i).toString());*/


Log.e("subTitle", subTitle.toString());

From what I can see, it is being passed the correct values, but it just isn't working. I keep getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.HashMap.containsKey(java.lang.Object)' on a null object reference


Answer Source

You need to initialize your HashMap HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>