android - Attempt to invoke interface method 'void com.bustracker.AsyncTaskCallback.onAsyncTaskFinished(java.util.ArrayList)' on a null object reference -
i trying pass result doinbackground onpostexecute() method getting error:
java.lang.nullpointerexception: attempt invoke interface method 'void com.bustracker.asynctaskcallback.onasynctaskfinished(java.util.arraylist)' on null object reference
what cant understand checking in onpostexecute
whether arraylist empty or not when output of routes the output of routes: [3,6,8]
app crashes when arraylist empty, app not crash?!
how can fix that?
i appreciate help.
postdata class:
public class postdata { string jsonstring; private asynctaskcallback callback; public postdata(asynctaskcallback callback) { this.callback = callback; } public string getjsonstring() { return jsonstring; } public void setjsonstring(string jsonstring) { this.jsonstring = jsonstring; } public void post_data(string jsonstring, context context) { this.jsonstring = jsonstring; new myasynctask(context).execute(jsonstring); // new myasynctask(context).cancel(true); } class myasynctask extends asynctask<string, integer, arraylist<integer>> { arraylist<integer> routes = new arraylist<integer>(); @override protected arraylist<integer> doinbackground(string... params) { // todo auto-generated method stub bufferedreader reader = null; try { url myurl = new url( "https://bustracker.rhcloud.com/webapi/test"); httpurlconnection conn = (httpurlconnection) myurl .openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(10000); conn.setreadtimeout(10000); conn.connect(); stringbuilder sb = new stringbuilder(); reader = new bufferedreader(new inputstreamreader( conn.getinputstream())); string line; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } system.out.println("stringbuilder: " +sb); gson gson = new gson(); jsonstore data = gson.fromjson(sb.tostring(), jsonstore.class); routes = data.getroutes(); system.out.println("the output of routes: " +routes); } catch (ioexception e) { e.printstacktrace(); return null; } { if (reader != null) { try { reader.close(); return null; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } } return null; } protected void onpostexecute(arraylist<integer> result) { if (routes != null && !routes.isempty()) { callback.onasynctaskfinished(routes); } else { log.e("123", "avoiding null pointer in availableroutes class, routes null!!!"); } } } }
java.lang.nullpointerexception: attempt invoke interface method 'void com.bustracker.asynctaskcallback.onasynctaskfinished(java.util.arraylist)' on null object reference
looks interface instance null. initialize asynctaskcallback instance.
Comments
Post a Comment