java - JSON null Object Reference Error {Android} -


i have made application tutorial http://javatechig.com/android/json-feed-reader-in-android

but when run app in android studio there following errors

caused by: java.lang.nullpointerexception: attempt invoke virtual method 'int java.lang.string.length()' on null object reference             @ org.json.jsontokener.nextcleaninternal(jsontokener.java:116)             @ org.json.jsontokener.nextvalue(jsontokener.java:94)             @ org.json.jsonobject.<init>(jsonobject.java:156)             @ org.json.jsonobject.<init>(jsonobject.java:173)             @ com.example.administrator.myapplication5.feedlistactivity.getjsonfromurl(feedlistactivity.java:127)             @ com.example.administrator.myapplication5.feedlistactivity$downloadfilestask.doinbackground(feedlistactivity.java:85)             @ com.example.administrator.myapplication5.feedlistactivity$downloadfilestask.doinbackground(feedlistactivity.java:67)             @ android.os.asynctask$2.call(asynctask.java:288)             @ java.util.concurrent.futuretask.run(futuretask.java:237)             at android.os.asynctask$serialexecutor$1.run(asynctask.java:231)             at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)             at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)             at java.lang.thread.run(thread.java:818) 

this feedlistactivity class

public class feedlistactivity extends activity {      private arraylist<feeditem> feedlist = null;     private progressbar progressbar = null;     private listview feedlistview = null;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_posts_list);         progressbar = (progressbar) findviewbyid(r.id.progressbar);         string url = "http://javatechig.com/api/get_category_posts/?dev=1&slug=android";         new downloadfilestask().execute(url);     }      public void updatelist() {         feedlistview= (listview) findviewbyid(r.id.custom_list);         feedlistview.setvisibility(view.visible);         progressbar.setvisibility(view.gone);          feedlistview.setadapter(new customlistadapter(this, feedlist));         feedlistview.setonitemclicklistener(new onitemclicklistener() {              @override             public void onitemclick(adapterview<?> a, view v, int position, long id) {                 object o = feedlistview.getitematposition(position);                 feeditem newsdata = (feeditem) o;                  intent intent = new intent(feedlistactivity.this, feeddetailsactivity.class);                 intent.putextra("feed", newsdata);                 startactivity(intent);             }         });     }      private class downloadfilestask extends asynctask<string, integer, void> {          @override         protected void onprogressupdate(integer... values) {         }          @override         protected void onpostexecute(void result) {             if (null != feedlist) {                 updatelist();             }         }          @override         protected void doinbackground(string... params) {             string url = params[0];              // getting json string url             jsonobject json = getjsonfromurl(url);              //parsing json data             parsejson(json);             return null;         }     }       public jsonobject getjsonfromurl(string url) {         inputstream = null;         jsonobject jobj = null;         string json = null;          // making http request         try {             // defaulthttpclient             defaulthttpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);              httpresponse httpresponse = httpclient.execute(httppost);             httpentity httpentity = httpresponse.getentity();             = httpentity.getcontent();              bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "iso-8859-1"), 8);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             jobj = new jsonobject(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      }      public void parsejson(jsonobject json) {         try {              // parsing json object             if (json.getstring("status").equalsignorecase("ok")) {                 jsonarray posts = json.getjsonarray("posts");                  feedlist = new arraylist<feeditem>();                  (int = 0; < posts.length(); i++) {                     jsonobject post = (jsonobject) posts.getjsonobject(i);                     feeditem item = new feeditem();                     item.settitle(post.getstring("title"));                     item.setdate(post.getstring("date"));                     item.setid(post.getstring("id"));                     item.seturl(post.getstring("url"));                     item.setcontent(post.getstring("content"));                     jsonarray attachments = post.getjsonarray("attachments");                      if (null != attachments && attachments.length() > 0) {                         jsonobject attachment = attachments.getjsonobject(0);                         if (attachment != null)                             item.setattachmenturl(attachment.getstring("url"));                     }                      feedlist.add(item);                 }             }         } catch (jsonexception e) {             e.printstacktrace();         }     }  } 

also, found answer (android strange error) didn't work

i thankful consider error , me solve that.

the exception thrown here:

try {     jobj = new jsonobject(json); } catch (jsonexception e) {     log.e("json parser", "error parsing data " + e.tostring()); } 

json null, , jsonobject can't created. please check why json not assigned , remains null in code:

string json = null;  // making http request try {     // defaulthttpclient     defaulthttpclient httpclient = new defaulthttpclient();     httppost httppost = new httppost(url);      httpresponse httpresponse = httpclient.execute(httppost);     httpentity httpentity = httpresponse.getentity();     = httpentity.getcontent();      bufferedreader reader = new bufferedreader(new inputstreamreader(             is, "iso-8859-1"), 8);     stringbuilder sb = new stringbuilder();     string line = null;     while ((line = reader.readline()) != null) {         sb.append(line + "\n");     }     is.close();     json = sb.tostring(); } catch (unsupportedencodingexception e) {     e.printstacktrace(); } catch (clientprotocolexception e) {     e.printstacktrace(); } catch (ioexception e) {     e.printstacktrace(); } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -