android - what is wrong with this piece of code?i am getting "null pointer exception".code is about drawing route on map -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
private string getmapsapidirectionsurl() { string origin; string destination=string.valueof(l.longitude)+","+string.valueof(l.latitude); origin=string.valueof(longitude)+","+string.valueof(latitude); // string url = "https://maps.googleapis.com/maps/api/directions/"+ output + "?" + params; string url="https://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&sensor=false"; return url; }
i getting nullpointerexception
. know not able identify causing it. assuming outside method correct, wrong syntactically?
this entire code:
package autogenie.maptrial; import android.manifest; import android.content.context; import android.content.pm.packagemanager; import android.graphics.color; import android.location.address; import android.location.geocoder; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.asynctask; import android.os.bundle; import android.os.handler; import android.os.message; import android.support.v4.app.activitycompat; import android.support.v4.app.fragmentactivity; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.google.android.gms.common.api.status; import com.google.android.gms.location.places.place; import com.google.android.gms.location.places.ui.placeautocompletefragment; import com.google.android.gms.location.places.ui.placeselectionlistener; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polylineoptions; import org.json.jsonobject; import java.io.ioexception; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.locale; public class mainactivity extends fragmentactivity implements locationlistener,googlemap.onmarkerdraglistener { private double latitude, longitude; public googlemap googlemap,googlemap1; locationmanager locationmanager; button addressbutton; placeautocompletefragment fragment; latlng l; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); supportmapfragment supportmapfragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); googlemap = supportmapfragment.getmap(); googlemap.setmaptype(googlemap.map_type_normal); googlemap.setmylocationenabled(true); addressbutton = (button) findviewbyid(r.id.addressbutton); fragment = (placeautocompletefragment) getfragmentmanager().findfragmentbyid(r.id.place_autocomplete_fragment); string url = getmapsapidirectionsurl(); readtask downloadtask = new readtask(); downloadtask.execute(url); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0, this); if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { // todo: consider calling // activitycompat#requestpermissions // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activitycompat#requestpermissions more details. return; } addressbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { edittext edittext = (edittext) findviewbyid(r.id.enter_place_name); string address = edittext.gettext().tostring(); geocodinglocation locationaddress = new geocodinglocation(); locationaddress.getaddressfromlocation(address, getapplicationcontext(), new geocoderhandler()); l= getlocationfromaddress(getapplicationcontext(), address); markeroptions a=new markeroptions().position(l); marker m=googlemap.addmarker(a); m.setposition(l); } }); fragment.setonplaceselectedlistener(new placeselectionlistener() { @override public void onplaceselected(place place) { // handle selected place } @override public void onerror(status status) { // handle error } }); } public latlng getlocationfromaddress(context context,string straddress) { geocoder coder = new geocoder(context,locale.getdefault()); list<address> address; latlng p1 = null; try { address = coder.getfromlocationname(straddress, 1); if (address == null) { return null; } address location = address.get(0); location.getlatitude(); location.getlongitude(); p1 = new latlng(location.getlatitude(), location.getlongitude() ); } catch (exception ex) { ex.printstacktrace(); } return p1; } private class geocoderhandler extends handler { @override public void handlemessage(message message) { string locationaddress; switch (message.what) { case 1: bundle bundle = message.getdata(); locationaddress = bundle.getstring("address"); break; default: locationaddress = null; } toast.maketext(getapplicationcontext(),locationaddress,toast.length_long).show(); } } @override public void onlocationchanged(location location) { if (location == null) { latitude = 28.608426; longitude = 77.225168; } else { longitude = location.getlongitude(); latitude = location.getlatitude(); } string s = "longitude:" + longitude + "latitude:" + latitude; log.i("current location", " latlong value" + s); googlemap.addmarker(new markeroptions() .position(new latlng(latitude, longitude)) .title("hello world")); /*cameraupdate center = cameraupdatefactory.newlatlng(new latlng(latitude, -longitude)); cameraupdate zoom = cameraupdatefactory.zoomto(10); googlemap.movecamera(center); googlemap.animatecamera(zoom);*/ } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } @override public void onmarkerdragstart(marker marker) { } @override public void onmarkerdrag(marker marker) { } @override public void onmarkerdragend(marker marker) { } //dfg private string getmapsapidirectionsurl() { // string url = "https://maps.googleapis.com/maps/api/directions/"+ output + "?" + params; string url="https://maps.googleapis.com/maps/api/directions/json?origin="+l+"&destination="+latitude+","+longitude+"&sensor=false"; return url; } private class readtask extends asynctask<string, void, string> { @override protected string doinbackground(string... url) { string data = ""; try { httpconnection http = new httpconnection(); data = http.readurl(url[0]); } catch (exception e) { log.d("background task", e.tostring()); } return data; } @override protected void onpostexecute(string result) { super.onpostexecute(result); new parsertask().execute(result); } } private class parsertask extends asynctask<string, integer, list<list<hashmap<string, string>>>> { @override protected list<list<hashmap<string, string>>> doinbackground( string... jsondata) { jsonobject jobject; list<list<hashmap<string, string>>> routes = null; try { jobject = new jsonobject(jsondata[0]); pathjsonparser parser = new pathjsonparser(); routes = parser.parse(jobject); } catch (exception e) { e.printstacktrace(); } return routes; } @override protected void onpostexecute(list<list<hashmap<string, string>>> routes) { arraylist<latlng> points = null; polylineoptions polylineoptions = null; // traversing through routes (int = 0; < routes.size(); i++) { points = new arraylist<latlng>(); polylineoptions = new polylineoptions(); list<hashmap<string, string>> path = routes.get(i); (int j = 0; j < path.size(); j++) { hashmap<string, string> point = path.get(j); double lat = double.parsedouble(point.get("lat")); double lng = double.parsedouble(point.get("lng")); latlng position = new latlng(lat, lng); points.add(position); } polylineoptions.addall(points); polylineoptions.width(2); polylineoptions.color(color.blue); } googlemap.addpolyline(polylineoptions); } } class geocodinglocation { private static final string tag = "geocodinglocation"; public string result1,result2; public void getaddressfromlocation(final string locationaddress, final context context, final handler handler) { thread thread = new thread() { @override public void run() { geocoder geocoder = new geocoder(context, locale.getdefault()); string result = null; try { list addresslist = geocoder.getfromlocationname(locationaddress, 1); if (addresslist != null && addresslist.size() > 0) { address address = (address) addresslist.get(0); stringbuilder sb = new stringbuilder(); sb.append(address.getlatitude()).append("\n"); sb.append(address.getlongitude()).append("\n"); result = sb.tostring(); } } catch (ioexception e) { log.e(tag, "unable connect geocoder", e); } { message message = message.obtain(); message.settarget(handler); if (result != null) { message.what = 1; bundle bundle = new bundle(); result = "address: " + locationaddress + "\n\nlatitude , longitude :\n" + result; bundle.putstring("address", result); message.setdata(bundle); } else { message.what = 1; bundle bundle = new bundle(); result = "address: " + locationaddress + "\n unable latitude , longitude address location."; bundle.putstring("address", result); message.setdata(bundle); } message.sendtotarget(); } } }; thread.start(); } } }
stack trace:
fatal exception: main java.lang.nullpointerexception @ com.google.maps.api.android.lib6.e.aw.<init>(unknown source) @ com.google.maps.api.android.lib6.e.ev.a(unknown source) @ com.google.android.gms.maps.internal.j.ontransact(sourcefile:137) @ android.os.binder.transact(binder.java:310) @ com.google.android.gms.maps.internal.igooglemapdelegate$zza$zza.addpolyline(unknown source) @ com.google.android.gms.maps.googlemap.addpolyline(unknown source) @ autogenie.maptrial.mainactivity$parsertask.onpostexecute(mainactivity.java:315) @ autogenie.maptrial.mainactivity$parsertask.onpostexecute(mainactivity.java:267) @ 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:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method) 01-21 12:24:35.291 3683-3683/autogenie.maptrial i/process: sending signal. pid: 3683 sig: 9
one of variables(l, latitude, longitude) null since place might cause exception valueof calls.
Comments
Post a Comment