1

I am trying to parse a JSONArray into and ArrayList in my android app. The PHP script correctly retuns the expected results, however the Java fails with a null pointer exception at resultsList.add(map)

public void agencySearch(String tsearch)    {
        // Setting the URL for the Search by Town
        String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php";
        // Building parameters for the search
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("City", tsearch));

        // Getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url_search_agency, params);

        for (int i = 0; i < json.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            try {
                JSONObject c = (JSONObject) json.get(i);
                //Fill map
                Iterator iter = c.keys();
                while(iter.hasNext())   {
                    String currentKey = (String) iter.next();
                    map.put(currentKey, c.getString(currentKey));
                }
                resultsList.add(map);

            }
            catch (JSONException e) {
                e.printStackTrace();

            }

        };

        MainActivity.setResultsList(resultsList);

    }
IrishCarBomb
  • 49
  • 1
  • 2
  • 11

2 Answers2

1

try like this may help you,

public void agencySearch(String tsearch)    {
        // Setting the URL for the Search by Town
        String url_search_agency = "http://www.infinitycodeservices.com/get_agency_by_city.php";
        // Building parameters for the search
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("City", tsearch));

        // Getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url_search_agency, params);

       ArrayList<HashMap<String, String>> resultsList = new  ArrayList<HashMap<String, String>>();

        for (int i = 0; i < json.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            try {
                JSONObject c = json.getJSONObject(position);
                //Fill map
               Iterator<String> iter = c.keys();
                while(iter.hasNext())   {
                    String currentKey = it.next();
                    map.put(currentKey, c.getString(currentKey));
                }
                resultsList.add(map);

            }
            catch (JSONException e) {
                e.printStackTrace();

            }

        };

        MainActivity.setResultsList(resultsList);

    }
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
  • Nice catch. My declaration for resultsList was elsewhere in the class code. This was my original definition ArrayList> resultsList; after adding the = new ArrayList>() to the end of that line the exception was gone. Thanks again. Maybe I should have gotten some sleep and checked it again before asking the question. Simple syntax error. Hopefully it will help someone else as well. – IrishCarBomb Nov 08 '14 at 13:25
1

Use custom method which convert your JSONArray to List instead of iterate and build List.

How to call :

try {
     ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String,String>>) toList(json);
} catch (JSONException e) {
     e.printStackTrace();
}

Convert json array to List :

private List toList(JSONArray array) throws JSONException {
    List list = new ArrayList();
    int size = array.length();
    for (int i = 0; i < size; i++) {
        list.add(fromJson(array.get(i)));
    }
    return list;
}

Convert json to Object :

private Object fromJson(Object json) throws JSONException {
    if (json == JSONObject.NULL) {
        return null;
    } else if (json instanceof JSONObject) {
        return jsonToMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
        return toList((JSONArray) json);
    } else {
        return json;
    }
}

Convert json to map :

public Map<String, String> jsonToMap(JSONObject object) throws JSONException {
    Map<String, String> map = new HashMap();
    Iterator keys = object.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        map.put(key, fromJson(object.get(key)).toString());
    }
    return map;
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67