Android JSON null object reference error -


i using stackexchange api in application returns response :

{      "items":[         {            "tags":[               "android",             "gridview",             "android-fragments",             "android-activity"          ],          "owner":{               "reputation":30,             "user_id":3303863,             "user_type":"registered",             "profile_image":"http://i.stack.imgur.com/44pcy.jpg?s=128&g=1",             "display_name":"chris.b",             "link":"http://stackoverflow.com/users/3303863/chris-b"          },          "is_answered":false,          "view_count":7,          "answer_count":0,          "score":0,          "last_activity_date":1429979620,          "creation_date":1429979620,          "question_id":29867827,          "link":"http://stackoverflow.com/questions/29867827/android-app-close-without-error-on-click-of-the-first-gridview-item",          "title":"android app close without error, on click of first gridview item"       },       {            "tags":[               "android",             "genymotion"          ],          "owner":{               "reputation":61,             "user_id":213199,             "user_type":"registered",             "accept_rate":54,             "profile_image":"https://www.gravatar.com/avatar/2f0b0c0e0e449255b5e2892b10b97ba4?s=128&d=identicon&r=pg",             "display_name":"stella",             "link":"http://stackoverflow.com/users/213199/stella"          },          "is_answered":false,          "view_count":7,          "answer_count":0,          "score":0,          "last_activity_date":1429979268,          "creation_date":1429979268,          "question_id":29867773,          "link":"http://stackoverflow.com/questions/29867773/unable-to-run-android-app-in-genymotion-emulator",          "title":"unable run android app in genymotion emulator"       }, 

and using following code string url :

public string readjson() {     httpclient client = new defaulthttpclient();     httpget httpget = new httpget(url);      httpresponse response = null;     try {         response = client.execute(httpget);     } catch (ioexception e) {         e.printstacktrace();     }     statusline statusline = response.getstatusline();     int statuscode = statusline.getstatuscode();     if (statuscode == 200) {         httpentity entity = response.getentity();         inputstream content = null;         try {             content = entity.getcontent();         } catch (ioexception e) {             e.printstacktrace();         }         bufferedreader reader = new bufferedreader(new inputstreamreader(content));         string line;         try {             while ((line = reader.readline()) != null) {                 builder.append(line);             }         } catch (ioexception e) {             e.printstacktrace();         }     } else {         //log.e(re.class.tostring(), "failed download file");     }     return builder.tostring(); } 

and using async task display questions in text view :

public class jsontask extends asynctask<string,string,jsonobject> {     private progressdialog pdialog;      @override     protected void onpreexecute() {         super.onpreexecute();         pdialog = new progressdialog(mainactivity.this);         pdialog.setmessage("getting data ...");         pdialog.setindeterminate(false);         pdialog.setcancelable(true);         pdialog.show();     }      @override     protected jsonobject doinbackground(string... params) {         try {             ob1 = new jsonobject(readjson());         } catch (jsonexception e) {             e.printstacktrace();         } catch (exception e) {             e.printstacktrace();         }         return ob1;     }      @override     protected void onpostexecute(jsonobject jsonobject) {         try {             mjsonarr = jsonobject.getjsonarray("items");             for(int i=0;i<20;i++)             {                 ob2 = mjsonarr.getjsonobject(i);                 holder+=ob2.getstring("title");             }             tv.settext(holder);             pdialog.dismiss();         } catch (jsonexception e) {             e.printstacktrace();         }     } } 

but getting error saying

"attempt invoke virtual method 'org.json.jsonobject org.json.jsonarray.getjsonobject(int)' on null object reference"

on these lines :

        mjsonarr = ob1.getjsonarray("items"); 

and

public class jsontask extends asynctask<string,string,jsonobject> 

how fix this? ,how access 'display name' 'owner' object in json response ?

thanks

there no ob1 in onpostexecute()

change

mjsonarr = ob1.getjsonarray("items"); 

to

mjsonarr = jsonobject.getjsonarray("items"); 

also add null check before carrying out operation on it

@override protected void onpostexecute(jsonobject jsonobject) {     try {         mjsonarr = jsonobject.getjsonarray("items");         if(mjsonarr != null)         {             for(int i=0;i<20;i++)             {                 ob2 = mjsonarr.getjsonobject(i);                 if(ob2 != null)                 {                     holder+=ob2.getstring("title");                 }             }         }         if(holder != null && holder.length() > 0)             tv.settext(holder);         else             tv.settext("error in fetching data");         pdialog.dismiss();     } catch (jsonexception e) {         e.printstacktrace();         tv.settext("error in fetching data");         pdialog.dismiss();     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -