android - Null pointer exception when location manager is called -


i trying use location manager in app when try use object of location manager null pointer exception thrown here snipet

 public class mainactivity extends activity {  locationmanager locationmanager; float[] dist; double rest; long res ; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     string servicestring = mainactivity.this.location_service;     locationmanager= (locationmanager)getsystemservice(servicestring);     string provider = locationmanager.gps_provider;     final location loc1=locationmanager.getlastknownlocation(provider);     string l = double.tostring(loc1.getlatitude());   //  log.d("message: ",l);     string netprovider=locationmanager.network_provider;      locationlistener = new locationlistener() {        // double lat1 = loc1.getlatitude();         //double  lon1=loc1.getlongitude();          @override         public void onlocationchanged(location loc1) {             /*location.distancebetween(lat1,lon1,loc1.getlatitude(),loc1.getlongitude(),dist);                 res+=(long)dist[0];*/                 toast.maketext(mainactivity.this,"location changed",toast.length_long).show();                 log.d("message: ", "location changed, " + loc1.getaccuracy() + " , " + loc1.getlatitude() + "," + loc1.getlongitude());          }          @override         public void onstatuschanged(string provider, int status, bundle extras) {          }          @override         public void onproviderenabled(string provider) {          }          @override         public void onproviderdisabled(string provider) {          }     };     locationmanager.requestlocationupdates(provider, 1000, 1, my); } 

please me resolve problem.

and logcat

process: com.example.pragadeeswaran.distancecalculator, pid: 9468     java.lang.runtimeexception: unable start activity componentinfo{com.example.pragadeeswaran.distancecalculator/com.example.pragadeeswaran.distancecalculator.mainactivity}: java.lang.nullpointerexception             @ android.app.activitythread.performlaunchactivity(activitythread.java:2252)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2301)             @ android.app.activitythread.access$800(activitythread.java:144)             @ android.app.activitythread$h.handlemessage(activitythread.java:1246)             @ android.os.handler.dispatchmessage(handler.java:106)             @ android.os.looper.loop(looper.java:136)             @ android.app.activitythread.main(activitythread.java:5196)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:602)             @ dalvik.system.nativestart.main(native method) 

getlastknownlocation() can return null should check it:

string l; if(loc1 != null)     l = double.tostring(loc1.getlatitude()); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -