json - Android null pointer exception when using Async task -


i have async task in class , is parsing json. when execute code, in logcat see message: fatal exception: asynctask #1

.

.

.

caused by: java.lang.nullpointerexception.

if want @ it, here code:

mainactivity.java

    protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);           new readjson().execute(there address here);           textview = (textview) findviewbyid(r.id.textview1);    }     //async task:     inputstream inputstream = null;     string result = null;     httpresponse response;     bufferedreader reader;     jsonobject jobject;     string ajsonstring1;     string ajsonstring2;       public class readjson extends asynctask<string, void, string> {         protected string doinbackground(string... url) {              defaulthttpclient   httpclient = new defaulthttpclient(new basichttpparams());             httppost httppost = new httppost(there same address here);             // depends on web service             httppost.setheader("content-type", "application/json");              try {                 response = httpclient.execute(httppost);             } catch (clientprotocolexception e) {                 // todo auto-generated catch block                     e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }                        httpentity entity = response.getentity();              try {                 inputstream = entity.getcontent();             } catch (illegalstateexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             // json utf-8 default beleive              try {                 reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);             } catch (unsupportedencodingexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             stringbuilder sb = new stringbuilder();              string line = null;             try {                 while ((line = reader.readline()) != null)                 {                     sb.append(line + "\n");                 }             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             result = sb.tostring();                try {                 jobject = new jsonobject(result);             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             try {                 ajsonstring1 = jobject.getstring("value");             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             try {                 ajsonstring2 = jobject.getstring("timestamp");             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }               return ajsonstring1;          }              protected void onpostexecute(string result) {                 textview.settext(ajsonstring1);               }      }   

can see problem here? have idea?

thanks in advance

edit: here logcat:

   04-25 16:27:06.851: w/system.err(4697):  @     android.os.asynctask$2.call(asynctask.java:287)    04-25 16:27:06.861: w/system.err(4697):  @ java.util.concurrent.futuretask.run(futuretask.java:234)    04-25 16:27:06.861: w/system.err(4697):  @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)    04-25 16:27:06.871: w/system.err(4697):  @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080)    04-25 16:27:06.871: w/system.err(4697):  @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573)    04-25 16:27:06.881: w/system.err(4697):  @ java.lang.thread.run(thread.java:856)    04-25 16:27:06.891: w/dalvikvm(4697): threadid=11: thread exiting uncaught exception (group=0x40a71930)    04-25 16:27:06.911: e/androidruntime(4697): fatal exception: asynctask #1    04-25 16:27:06.911: e/androidruntime(4697): java.lang.runtimeexception: error occured while executing doinbackground()    04-25 16:27:06.911: e/androidruntime(4697):  @ android.os.asynctask$3.done(asynctask.java:299)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.futuretask.setexception(futuretask.java:219)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.futuretask.run(futuretask.java:239)    04-25 16:27:06.911: e/androidruntime(4697):  @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.lang.thread.run(thread.java:856)    04-25 16:27:06.911: e/androidruntime(4697): caused by: java.lang.nullpointerexception    04-25 16:27:06.911: e/androidruntime(4697):  @ com.example.ayasms.mainactivity$readjson.doinbackground(mainactivity.java:189)    04-25 16:27:06.911: e/androidruntime(4697):  @ com.example.ayasms.mainactivity$readjson.doinbackground(mainactivity.java:1)    04-25 16:27:06.911: e/androidruntime(4697):  @ android.os.asynctask$2.call(asynctask.java:287)    04-25 16:27:06.911: e/androidruntime(4697):  @ java.util.concurrent.futuretask.run(futuretask.java:234)    04-25 16:27:06.911: e/androidruntime(4697):  ... 4 more  

i assume try catch error handling inside asyncjob problem.

for example if line httpclient.execute(httppost);thrown exception catch , print stderr (which partly visible in logcat) continue!

of course without having executed httpclient.execute(..) have no response , therefore getting nullpointerexception.

conclusion: pack inside asyncjob in 1 try catch environment. furthermore should investigate class fails first , find out why.

try {     response = httpclient.execute(httppost);     httpentity entity = response.getentity();     inputstream = entity.getcontent();     reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);     stringbuilder sb = new stringbuilder();      string line = null;     while ((line = reader.readline()) != null) {     sb.append(line + "\n");     }     result = sb.tostring();     jobject = new jsonobject(result);       ajsonstring1 = jobject.getstring("value");     ajsonstring2 = jobject.getstring("timestamp");     return ajsonstring1; } catch (clientprotocolexception e) {     e.printstacktrace(); } catch (ioexception e) {     e.printstacktrace(); } catch (illegalstateexception e) {     e.printstacktrace(); } catch (unsupportedencodingexception e) {     e.printstacktrace();   } catch (jsonexception e) {     e.printstacktrace(); }            return null; // error case 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -