android - httpurlconnection.getinputstream NullPointerException -


i'm having trouble using httpurlconnection. when i'm using url= https://stackoverflow.com/ works well. use other code example: url= http://192.168.78.1/index.php doesn't work , results in nullpointerexception . can explain me why happens ? thank helping.

@override protected string doinbackground(void... params) {     try {         url url=new url("http://stackoverflow.com");         httpurlconnection connection=(httpurlconnection)url.openconnection();         connection.setrequestmethod("get");         connection.setdooutput(true);         connection.setdoinput(true);         connection.connect();          bufferedreader reader = new bufferedreader(new inputstreamreader(connection.getinputstream()));         stringbuilder stringbuilder = new stringbuilder();          string line = null;         while ((line = reader.readline()) != null)         {             stringbuilder.append(line + "\n");         }         connection.disconnect();         return  stringbuilder.tostring();     } catch (malformedurlexception e) {         return null;     } catch (protocolexception e) {         return null;     } catch (ioexception e) {         return null;     } }  @override protected void onpostexecute(string s) {     super.onpostexecute(s);      textview tv=(textview)context.findviewbyid(r.id.textview);     tv.settext(s.tostring()); } 

and stacktrace logcat:

1335-1335/com.example.administrator.http e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception         @ com.example.administrator.http.asysnc.onpostexecute(asysnc.java:57)         @ com.example.administrator.http.asysnc.onpostexecute(asysnc.java:15)         @ android.os.asynctask.finish(asynctask.java:631)         @ android.os.asynctask.access$600(asynctask.java:177)         @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644)         @ android.os.handler.dispatchmessage(handler.java:99)         @ android.os.looper.loop(looper.java:137)         @ android.app.activitythread.main(activitythread.java:4745)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:511)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553)         @ dalvik.system.nativestart.main(native method) 

you got nullpointerexception in void onpostexecute(string s) mehtod. happened, because string doinbackground(void... params) mehtod returned null. returned null, because, got exception inside method , code returns null every time when exception occurs. exception may occur because of different reasons - e.g.:

  • connection time out
  • incorrect url
  • unreachable server or end-point
  • malformed data in request or response
  • lost internet connection
  • input/output error
  • and on...

i think, got error, because url http://192.168.78.1/index.php unreachable android device. maybe in different network, address unavailable in network, ip incorrect or that, it's guess. don't have access environment, cannot debug it.

asynctask unfortunately doesn't provide error handling mechanisms. can perform null-checks in void onpostexecute(string s) method or try other "tricks" prevent such situations, in opinion, they're workarounds. if want have clean , robust code, should rid of asynctask re-implement solution using e.g. rxjava, has great support error handling , other useful features. should aware of situations, when server in unreachable, because happens in real life.


Comments