android - NullPointerException parsing JSON data -


i'm trying parse json, i'm using this example.

in example in class parser not used secondary thread , networkonmainthreadexception.

to solve problem have introduced code in asynctask not transfer data since error on line:

contacts = json.getjsonarray(tag_contacts);

this simple code using

jsonparser.java

public class jsonparser {      static inputstream = null;     static jsonobject jobj = null;     static string json = "";      // constructor     public jsonparser() {      }      public jsonobject getjsonfromurl(string url) {         new parsejson().execute(url);         return jobj;     }      private class parsejson extends asynctask<string, void, jsonobject>{           protected jsonobject doinbackground(string... params) {              // making http request              try {                  // defaulthttpclient                  defaulthttpclient httpclient = new defaulthttpclient();                  httppost httppost = new httppost(params[0]);                   httpresponse httpresponse = httpclient.execute(httppost);                  httpentity httpentity = httpresponse.getentity();                  = httpentity.getcontent();                         } catch (unsupportedencodingexception e) {                  e.printstacktrace();              } catch (clientprotocolexception e) {                  e.printstacktrace();              } catch (ioexception e) {                  e.printstacktrace();              }               try {                  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 (exception e) {                  log.e("buffer error", "error converting result " + e.tostring());              }               // try parse string json object              try {                  jobj = new jsonobject(json);              } catch (jsonexception e) {                  log.e("json parser", "error parsing data " + e.tostring());              }               // return json string              return jobj;          }    } } 

mainactivity.java

public class mainactivity extends activity{  // url make request private static string url = "http://api.androidhive.info/contacts/";  // json node names private static final string tag_contacts = "contacts"; private static final string tag_id = "id"; private static final string tag_name = "name";   // contacts jsonarray jsonarray contacts = null; jsonobject json;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     new task().execute(); }  public void loadareas(){     try {          // looping through contacts         for(int = 0; < contacts.length(); i++){             jsonobject c = contacts.getjsonobject(i);              // storing each json item in variable             string id = c.getstring(tag_id);             string name = c.getstring(tag_name);              log.e("tag","id: "+id+" - "+"name: "+name);         }     } catch (jsonexception e) {         e.printstacktrace();     } }            private class task extends asynctask<string, void, string> {      @override     protected string doinbackground(string... params) {           try {         // creating json parser instance         jsonparser jparser = new jsonparser();          // getting json string url         jsonobject json = jparser.getjsonfromurl(url);          // getting array of contacts             contacts = json.getjsonarray(tag_contacts);          } catch (jsonexception e) {             e.printstacktrace();         }         return null;     }     @override     protected void onpostexecute(string result) {         loadareas();     }  } 

}

myjsondata

{     "contacts": [         {                 "id": "c200",                 "name": "ravi tamada",                 "email": "ravi@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c201",                 "name": "johnny depp",                 "email": "johnny_depp@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c202",                 "name": "leonardo dicaprio",                 "email": "leonardo_dicaprio@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c203",                 "name": "john wayne",                 "email": "john_wayne@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c204",                 "name": "angelina jolie",                 "email": "angelina_jolie@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "female",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c205",                 "name": "dido",                 "email": "dido@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "female",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c206",                 "name": "adele",                 "email": "adele@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "female",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c207",                 "name": "hugh jackman",                 "email": "hugh_jackman@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c208",                 "name": "will smith",                 "email": "will_smith@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c209",                 "name": "clint eastwood",                 "email": "clint_eastwood@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c2010",                 "name": "barack obama",                 "email": "barack_obama@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c2011",                 "name": "kate winslet",                 "email": "kate_winslet@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "female",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         },         {                 "id": "c2012",                 "name": "eminem",                 "email": "eminem@gmail.com",                 "address": "xx-xx-xxxx,x - street, x - country",                 "gender" : "male",                 "phone": {                     "mobile": "+91 0000000000",                     "home": "00 000000",                     "office": "00 000000"                 }         }     ] } 

logcat

04-01 16:06:48.414: w/dalvikvm(9151): threadid=11: thread exiting uncaught exception (group=0x40d09930) 04-01 16:06:48.422: e/androidruntime(9151): fatal exception: asynctask #1 04-01 16:06:48.422: e/androidruntime(9151): java.lang.runtimeexception: error occured while executing doinbackground() 04-01 16:06:48.422: e/androidruntime(9151):     @ android.os.asynctask$3.done(asynctask.java:299) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.futuretask.run(futuretask.java:239) 04-01 16:06:48.422: e/androidruntime(9151):     @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.lang.thread.run(thread.java:856) 04-01 16:06:48.422: e/androidruntime(9151): caused by: java.lang.nullpointerexception 04-01 16:06:48.422: e/androidruntime(9151):     @ com.example.json_parser.mainactivity$task.doinbackground(mainactivity.java:63) 04-01 16:06:48.422: e/androidruntime(9151):     @ com.example.json_parser.mainactivity$task.doinbackground(mainactivity.java:1) 04-01 16:06:48.422: e/androidruntime(9151):     @ android.os.asynctask$2.call(asynctask.java:287) 04-01 16:06:48.422: e/androidruntime(9151):     @ java.util.concurrent.futuretask.run(futuretask.java:234) 04-01 16:06:48.422: e/androidruntime(9151):     ... 4 more 04-01 16:06:48.461: d/libegl(9151): loaded /vendor/lib/egl/libegl_powervr_sgx540_120.so 04-01 16:06:48.476: d/libegl(9151): loaded /vendor/lib/egl/libglesv1_cm_powervr_sgx540_120.so 04-01 16:06:48.484: d/libegl(9151): loaded /vendor/lib/egl/libglesv2_powervr_sgx540_120.so 04-01 16:06:48.570: d/openglrenderer(9151): enabling debug mode 0 04-01 16:06:50.273: d/dalvikvm(9151): gc_concurrent freed 182k, 3% free 8885k/9100k, paused 3ms+23ms, total 41ms 

that problem have?

i incorrectly returning json object?

thanks!

an asynctask, name implies, executes task asynchronously. means not block execution, so:

    jsonparser jparser = new jsonparser();      // getting json string url     jsonobject json = jparser.getjsonfromurl(url); 

the value returning jparser.getjsonfromurl(url) null. asynctask's doinbackground() hasn't executed yet.

you need move parsing code doinbackground(), move code updates ui onpostexecute().


Comments

Popular posts from this blog

python - cx_oracle unable to find Oracle Client -

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -