2

How can i iterate over all of the "listPages" in this JSON object?

 { "listPages": [ { "title": "Accounts", "recordType": "Company", }, { "title": "Contacts", "recordType": "Person", } ] } 

I'm trying to add list items to a list from each item in the listPages array via this code:

 JSONObject JSONConfig = envConfig.getEnvConfig(this); try{ JSONArray listPages = JSONConfig.getJSONArray("listPages"); for(int i = 0 ; i < listPages.length() ; i++){ listItems.add(listPages.getJSONObject(i).getString("title")); } adapter.notifyDataSetChanged(); }catch(Exception e){ e.printStackTrace(); } 

I can see in logcat that i'm getting a system error: "java.lang.NullPointerException" on the following line.

JSONArray listPages = JSONConfig.getJSONArray("listPages"); 

I've tried reading and tweaking things from other questions but i can't figure it out. Help would be much appreciated.

here is my envConfig.java class

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.json.JSONObject; import android.content.Context; import android.util.Log; public class EnvConfig { private String rawJSONString; private JSONObject jsonObjRecv; public JSONObject getEnvConfig(Context context){ InputStream inputStream = context.getResources().openRawResource( R.raw.envconfigg); BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } rawJSONString = sb.toString(); try { JSONObject jsonObjRecv = new JSONObject(rawJSONString); Log.i("Test", "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>"); } catch (Exception e) { e.printStackTrace(); } return jsonObjRecv; } } 
9
  • Where is the code for envConfig.getEnvConfig(this);? Have you verified that it is returning a valid JSONArray object? Commented Mar 6, 2013 at 2:05
  • yes. first thing i did was run it through json lint Commented Mar 6, 2013 at 2:06
  • 1
    jsonlint is an external tools. I am referring to the method in your code - envConfig.getEnvConfig(this);. Print out the value you get from this method prior to entering your for loop. Commented Mar 6, 2013 at 2:10
  • yeah, i'm logging it to logcat and everything looks fine when its being logged. Commented Mar 6, 2013 at 2:34
  • It seems to me that the JSONObject returned in the first line is probably null. Meaning you never successfully parsed the JSON. Commented Mar 6, 2013 at 2:48

2 Answers 2

1

This is a classic problem of instance shadowing. You declare a new variable in your method, in the try block, with the same name as a class variable. So, the class variable is shadowed and thus, never initialized. When you later return it from the method, its null.

public class EnvConfig { private String rawJSONString; private JSONObject jsonObjRecv; // <-- you declare a class variable here // ... try { JSONObject jsonObjRecv = new JSONObject(rawJSONString); // <-- shadowed here! 

Unless you are trying to avoid re-parsing the JSON repeatedly, I would advise getting rid of the class variables altogether. Otherwise, get rid of the local variable.

Sign up to request clarification or add additional context in comments.

1 Comment

ahhhh i see that now. thanks! i've never even heard of instance shadowing. Thanks for the enlightenment!
0

Here is the code that I use for Parsing my JSON Data, I am not familiar with the JSONConfig that you are using, however this works prefectly for me.

JSONObject jsonObject = (JSONObject) new JSONTokener(/*Json String Data*/).nextValue(); JSONArray jsonArray = jsonObject.getJSONArray(/*Name of JSON Array*/); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.