java - getting a null pointer exception when trying to parse JSON data from a url in a Async Class android -


could please explain or correct why getting null pointer exception in async class? trying data url null pointer exception 162, contains following code

int lengthjsonarr = jsonmainnode.length();

i not sure why if great. or if can show me better alternative fetch json data url great help.

public class usertask extends asynctask<string, void, void>{     httpurlconnection connection = null;     private string content;     @override     protected void doinbackground(string... urls) {          bufferedreader reader = null;         try {             url url = new url(urls[0]);             connection = (httpurlconnection) url.openconnection();             connection.connect();              inputstream stream = connection.getinputstream();              reader = new bufferedreader(new inputstreamreader(stream));              stringbuffer buffer = new stringbuffer();              string line = "";             while ((line = reader.readline()) != null) {                 buffer.append(line);             } content = buffer.tostring();           } catch (malformedurlexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } {             if (connection != null) {                 connection.disconnect();             }             try {                 if (reader != null) {                     reader.close();                 }             } catch (ioexception e) {                 e.printstacktrace();             }          }         return null;     }      @override     protected void onpostexecute(void s) {         super.onpostexecute(s);          string outputdata = "";         jsonobject jsonresponse;          try {             jsonresponse = new jsonobject(content);             jsonarray jsonmainnode = jsonresponse.optjsonarray("android");              int lengthjsonarr = jsonmainnode.length(); //this causing exception              (int =0; < lengthjsonarr; i++) {                 jsonobject jsonchildnode = jsonmainnode.getjsonobject(i);                 string name = jsonchildnode.optstring("name").tostring();                 double longitude = jsonchildnode.optdouble("lon");                 double latitude = jsonchildnode.optdouble("lat");                  outputdata += " name : "+ name +" "                         + "longitude : "+ longitude +" "                         + "latitude  : "+ latitude +" "                         +"-------------------------------------------------- ";                  //show parsed output on screen (activity)                 toast toast = toast.maketext(getapplicationcontext(), outputdata, toast.length_long);                 toast.show();               }         } catch (jsonexception e) {             e.printstacktrace();         }     } } 

this not way fetch json data in android. should use volley or retrofit library. these libraries work accuratly , efficiently normal code.

there alot of things take care of while fetching data. done library. , need write few lines of code.

you can follow many tutorials on google.


Comments