0

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 ?

3
  • 1
    mSubList is null you need to initialize it before adding map Commented Jan 10, 2015 at 15:25
  • 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 ? Commented Jan 10, 2015 at 15:59
  • @akshey: if you want to change ListView rows color then create custom adapter by extending BaseAdpter,SimpleAdapter,.. . currently you are using default SimpleAdapter implementation so it's not possible without creating custom adapter Commented Jan 10, 2015 at 16:03

3 Answers 3

2
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>>();
Sign up to request clarification or add additional context in comments.

1 Comment

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 ?
1

init your mSubList before add item

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

Comments

1

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

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

Comments

Your Answer

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.