MohanRaj S MohanRaj S - 9 days ago 7
Android Question

Android JSON parsing Json Array is [] Throws Null Pointer Exception while parsing, How to write in Proper way?

I have complex API that i parse and show in list view,I will be struggle to parse JSONArray.Here i will be in struggle following Json Array which is inside the posts json object "tags_name": ["Activities"],,some object it will come like "tags_name": [], this.Kindly review my question. My API and code is below. Presently i will implemented parsing code with model class. Once fix this issue i have to write list view coding please help me. May be my question formation is in silly way. please it look like means give some suggestion to frame question. Thanks in Advance.

MyAPI:

{
"status": true,
"nextpage": 0,
"count": 31,
"data": {
"postlist": [{
"posts": {
"pm_post_id": "4668",
"pm_user_id": "6022",
"pm_type": "1",
"pm_title": "",
"pm_description": "Team Members in Tradezap",
"pm_likes": "1",
"pm_comments": "0",
"pm_contributors": "0",
"pm_report": "0",
"pm_views": "0",
"pm_created_on": "2016-11-14 18:49:58",
"pm_updated_on": "0000-00-00 00:00:00",
"pm_shared_type": "1",
"pm_shared_with": "0",
"pm_tags": "0,230,0,0",
"pm_timeline": "1",
"pm_step": "1",
"pm_status": "1",
"pm_epid": "0",
"pm_group_id": "0"
},
"tags_name": [
"Activities"
],
"images_count": 3,
"images": [{
"pg_gallery_id": "5581",
"pg_name": "IMG_5201.JPG",
"pg_link": "",
"pg_type": "1",
"pg_date": "2016-11-14 18:49:58"
}, {
"pg_gallery_id": "5582",
"pg_name": "IMG_5192.JPG",
"pg_link": "",
"pg_type": "1",
"pg_date": "2016-11-14 18:49:58"
}, {
"pg_gallery_id": "5583",
"pg_name": "IMG_5195.JPG",
"pg_link": "",
"pg_type": "1",
"pg_date": "2016-11-14 18:49:58"
}],
"post_user": [{
"first_name": "Michelle",
"last_name": "Smith",
"profile_pic": "profess_sw_engg.jpg"
}],
"is_encourage_user": true,
"encourage_feed_id": "1093"
},

{
"posts": {
"pm_post_id": "4647",
"pm_user_id": "6022",
"pm_type": "1",
"pm_title": "",
"pm_description": "Test Post Data without images",
"pm_likes": "1",
"pm_comments": "2",
"pm_contributors": "1",
"pm_report": "0",
"pm_views": "0",
"pm_created_on": "2016-10-16 02:16:35",
"pm_updated_on": "0000-00-00 00:00:00",
"pm_shared_type": "1",
"pm_shared_with": "0",
"pm_tags": "0,99,0,0",
"pm_timeline": "1",
"pm_step": "0",
"pm_status": "1",
"pm_epid": "0",
"pm_group_id": "0"
},
"tags_name": [
"Activity Based"
],
"images_count": 0,
"images": [],
"post_user": [{
"first_name": "Michelle",
"last_name": "Smith",
"profile_pic": "profess_sw_engg.jpg"
}],
"is_encourage_user": true,
"encourage_feed_id": "992"
}

],
"path_large": "uploads/dashboard/large/",
"path_medium": "uploads/dashboard/medium/",
"path_small": "uploads/dashboard/small/",
"path_thumb": "uploads/dashboard/thumb/"
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJpZCI6IjYwMjIiLCJ1c2VybmFtZSI6ImlvYW5hc2FsYS1zZXRpdSJ9LCJpYXQiOjE0ODAxNDc3MTQsImp0aSI6IlZXMHliWFJEVFdobWVuUnJRV2gwVFRkWWRVYz0iLCJpc3MiOiJodHRwOlwvXC9kZW1vLnlvdXRoaHViLmNvLm56XC8iLCJuYmYiOjE0ODAxNDc3MTQsImV4cCI6MTQ4MDc1MjUxNH0.GJSsptap7_VxayEBBavm1EsiZezgZMxW9hxSwtI4gAE"
}


My Method for Parsing

private void parsingPostValues(String responseStatus) throws JSONException {
JSONObject responseObject = new JSONObject(responseStatus);
JSONObject datObject = responseObject.getJSONObject("data");
JSONArray postArr = new JSONArray(datObject.getString("postlist"));
for (int i = 0; i < postArr.length(); i++) {
JSONObject tempPostObject = postArr.getJSONObject(i);
JSONObject postObject = tempPostObject.getJSONObject("posts");

//setTag Array- this is the functional area i'm in bottle-neck.
try {
JSONArray tagNameArr = tempPostObject.getJSONArray("tags_name");
//ArrayList<Bean> tagListdata = new ArrayList<Bean>(tagNameArr.length());
if (tagNameArr.length()>0) {
for (int tagInfo = 0; tagInfo < tagNameArr.length(); tagInfo++) {
// listdata.add(tagNameArr.get(i).toString());
String tagme = "";
//Bean tagBean = new Bean();
//tagBean.setTagsArray((tagme.isEmpty() ? tagNameArr.get(tagInfo).toString() : "null")); //tagBean.setTagsArray(tagNameArr.get(tagInfo).toString());
//tagListdata.add(tagBean);
//beanAccess.setTagsArray(tagNameArr.get(tagInfo));
System.out.println("Tags Array:"+tagInfo+":"+tagNameArr.get(tagInfo));
}
//beanAccess.setTagsArray(tagListdata);
}

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

}

Answer

replace this

JSONArray postArr = new JSONArray(datObject.getString("postlist"));

To

JSONArray postArr = datObject.getJSONArray("postlist");

Replace

String imgCount = tempPostObject.getString("images_count");
String is_encourage_user = tempPostObject.getString("is_encourage_user");

To

String imgCount = postObject.getString("images_count");
String is_encourage_user = postObject.getString("is_encourage_user");

It will work for you.