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);
 }
asked Nov 8, 2014 at 6:57
4

2 Answers 2

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);
 }
answered Nov 8, 2014 at 7:10
Sign up to request clarification or add additional context in comments.

1 Comment

Nice catch. My declaration for resultsList was elsewhere in the class code. This was my original definition ArrayList<Map<String, String>> resultsList; after adding the = new ArrayList<Map<String, String>>() 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.
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;
}
answered Nov 8, 2014 at 7:11

2 Comments

Have you try this @IrishCarBomb ?
I didn't. Is this using the GSON LIB?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.