akshay bhange
akshay bhange

Reputation: 2504

how to parse JSONArray in android NullPointerException

I want to parse jsonObject passed by another activity

intent.putextra("result", json.toString());

It shows everything right with name, branch and session. but it shows NullPointerException while fetching array. at here

            mSubList.add(map);

this array has 6 rows. this is my code. please tell me exactly where Im wrong.

JSONObject json;
ListView sub_list;

private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mSubList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result_page);

    name = (TextView)findViewById(R.id.name_roll);
    branch= (TextView)findViewById(R.id.branchname);
    session = (TextView)findViewById(R.id.Session);

    sub_list = (ListView)findViewById(R.id.sub_list);



    try {
         json = new JSONObject(getIntent().getStringExtra("result"));

        name.setText(json.getString("REGNO")+" - "+json.getString("STUDENTNAME"));
        branch.setText(json.getString("BRANCHNAME"));
        session.setText(json.getString("SESSIONNAME"));

        mComments = json.getJSONArray("DATA");

        // looping through all subjects according to the json object returned
        for (int i = 0; i < mComments.length(); i++) {
            JSONObject c = mComments.getJSONObject(i);

            // gets the content of each tag
            String code = c.getString("CCODE");
            String subject = c.getString("COURSENAME");
            String passfail = c.getString("PASSFAIL");

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put("CCODE", code);
            map.put("COURSENAME", subject);
            map.put("PASSFAIL", passfail);
            // adding HashList to ArrayList
            mSubList.add(map);
        }

                ListAdapter adapter = new SimpleAdapter(this, mSubList,
                R.layout.singlesubject, new String[] { "CCODE", "COURSENAME",
                "PASSFAIL" }, new int[] { R.id.sub_id, R.id.sub_name,R.id.sub_res });

        sub_list.setAdapter(adapter);
    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

if I want to change each listitem background(R.layout.singlesubject) if "PASSFAIl"=P then Green color else Red color. then what changes i have to do ?

Upvotes: 0

Views: 60

Answers (3)

Max Klein
Max Klein

Reputation: 528

I think you forgot to initialize mSubList before adding data to it:

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

Upvotes: 1

SorryForMyEnglish
SorryForMyEnglish

Reputation: 1181

init your mSubList before add item

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

Upvotes: 1

Josh Engelsma
Josh Engelsma

Reputation: 2646

mSubList.add(map);

this line of code is failing because you do not initialize the array before adding the map

You can initialize it by using...

mSubList = new ArrayList<Hashmap<String, String>>();

Upvotes: 2

Related Questions