java - NullPointerException when using GMapsV2Direction class -
i've been trying fix run-time error last few days no avail, not sure why it's throwing nullpointerexception when creates nodelist when getdirection method called. i'm new android programming if can offer me little guidance appreciated, thank you!
edit: judging debugger latlng co-ordinates , direction type being passed through getdocument() method, document isn't being returned getdocument() method.
public document getdocument(latlng start, latlng end, string mode) { string url = "http://maps.googleapis.com/maps/api/directions/xml?" + "origin=" + start.latitude + "," + start.longitude + "&destination=" + end.latitude + "," + end.longitude + "&sensor=false&units=metric&mode=driving"; try { httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httppost httppost = new httppost(url); httpresponse response = httpclient.execute(httppost, localcontext); inputstream in = response.getentity().getcontent(); documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder(); document doc = builder.parse(in); return doc; } catch (exception e) { e.printstacktrace(); } return null; }
.
public arraylist<latlng> getdirection (document doc) { nodelist nl1, nl2, nl3, nl4; arraylist<latlng> listgeopoints = new arraylist<latlng>(); double time=0; nl1 = doc.getelementsbytagname("step"); if (nl1.getlength() > 0) { (int = 0; < nl1.getlength(); i++) { double t=0; node node1 = nl1.item(i); nl2 = node1.getchildnodes(); node locationnode = nl2.item(getnodeindex(nl2, "start_location")); nl3 = locationnode.getchildnodes(); node latnode = nl3.item(getnodeindex(nl3, "lat")); double lat = double.parsedouble(latnode.gettextcontent()); node lngnode = nl3.item(getnodeindex(nl3, "lng")); double lng = double.parsedouble(lngnode.gettextcontent()); listgeopoints.add(new latlng(lat, lng)); locationnode = nl2.item(getnodeindex(nl2, "polyline")); nl3 = locationnode.getchildnodes(); latnode = nl3.item(getnodeindex(nl3, "points")); arraylist<latlng> arr = decodepoly(latnode.gettextcontent()); for(int j = 0 ; j < arr.size() ; j++) { listgeopoints.add(new latlng(arr.get(j).latitude, arr.get(j).longitude)); } locationnode = nl2.item(getnodeindex(nl2, "end_location")); nl3 = locationnode.getchildnodes(); latnode = nl3.item(getnodeindex(nl3, "lat")); lat = double.parsedouble(latnode.gettextcontent()); lngnode = nl3.item(getnodeindex(nl3, "lng")); lng = double.parsedouble(lngnode.gettextcontent()); listgeopoints.add(new latlng(lat, lng)); locationnode = nl2.item(getnodeindex(nl2, "duration")); nl4 = locationnode.getchildnodes(); node node2 = nl4.item(getnodeindex(nl4, "value")); t = double.parsedouble(node2.gettextcontent()); time=time+t; } log.i("duration", string.valueof(time)); } return listgeopoints; }
here function i'm using call getdirection method:
public void onclick_getdirections(view v){ mygpstooldirections md = new mygpstooldirections(); latlng fromposition = new latlng(13.68714, 100.53525); latlng toposition = new latlng(13.68366, 100.53900); document doc1 = md.getdocument(fromposition, toposition, mygpstooldirections.mode_driving); arraylist<latlng> directionpoint = md.getdirection(doc1); polylineoptions rectline = new polylineoptions().width(3).color(color.red); for(int = 0 ; < directionpoint.size() ; i++) { rectline.add(directionpoint.get(i)); gpstoolmap.addpolyline(rectline); }
stack trace:
08-07 17:14:31.682: e/androidruntime(2201): fatal exception: main 08-07 17:14:31.682: e/androidruntime(2201): java.lang.illegalstateexception: not execute method of activity 08-07 17:14:31.682: e/androidruntime(2201): @ android.view.view$1.onclick(view.java:3599) 08-07 17:14:31.682: e/androidruntime(2201): @ android.view.view.performclick(view.java:4204) 08-07 17:14:31.682: e/androidruntime(2201): @ android.view.view$performclick.run(view.java:17355) 08-07 17:14:31.682: e/androidruntime(2201): @ android.os.handler.handlecallback(handler.java:725) 08-07 17:14:31.682: e/androidruntime(2201): @ android.os.handler.dispatchmessage(handler.java:92) 08-07 17:14:31.682: e/androidruntime(2201): @ android.os.looper.loop(looper.java:137) 08-07 17:14:31.682: e/androidruntime(2201): @ android.app.activitythread.main(activitythread.java:5041) 08-07 17:14:31.682: e/androidruntime(2201): @ java.lang.reflect.method.invokenative(native method) 08-07 17:14:31.682: e/androidruntime(2201): @ java.lang.reflect.method.invoke(method.java:511) 08-07 17:14:31.682: e/androidruntime(2201): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-07 17:14:31.682: e/androidruntime(2201): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-07 17:14:31.682: e/androidruntime(2201): @ dalvik.system.nativestart.main(native method) 08-07 17:14:31.682: e/androidruntime(2201): caused by: java.lang.reflect.invocationtargetexception 08-07 17:14:31.682: e/androidruntime(2201): @ java.lang.reflect.method.invokenative(native method) 08-07 17:14:31.682: e/androidruntime(2201): @ java.lang.reflect.method.invoke(method.java:511) 08 -07 17:14:31.682: e/androidruntime(2201): @ android.view.view$1.onclick(view.java:3594) 08-07 17:14:31.682: e/androidruntime(2201): ... 11 more 08-07 17:14:31.682: e/androidruntime(2201): caused by: java.lang.nullpointerexception 08-07 17:14:31.682: e/androidruntime(2201): @ com.l00081183.mygpstool.mygpstooldirections.getdirection(mygpstooldirections.java:54) 08-07 17:14:31.682: e/androidruntime(2201): @ com.l00081183.mygpstool.mainactivity.onclick_getdirections(mainactivity.java:145)
Comments
Post a Comment