Great! You are correct, Mark. I used LogCat The NullPointerException is getting generated on line 34 which has getLastKnowLocation() method.
Now I updated the code and even then the same error comes : LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener ll = new mylocationlistener(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); Location l = lm.getLastKnownLocation(lm.getProvider(LOCATION_SERVICE).getName()); Log.w("LocationTracker", "The value of Location returned by getLastKnownLocation() is : " + l.toString()); Log.d("Current Location", l.getLatitude() + ""); Log.d("Current Location",l.getLongitude() + ""); Toast.makeText(this, l.getLatitude() + "" + l.getLongitude(),Toast.LENGTH_LONG).show(); Thanks again. It would be great if you could guide me further. Regards, prayag On May 5, 2:24 pm, Mark Murphy <mmur...@commonsware.com> wrote: > On Thu, May 5, 2011 at 2:20 PM, Prayag Pathak <prayag.d.pat...@gmail.com> > wrote: > > I am trying to get the latitude and longitude of the current location. > > Let me clarify, I dont want to implement a listener that will create > > an event when the location will be changed. > > You act as though you have a choice in the matter. > > > > > > > I am trying to create an > > app that will record the current location on a button click event. > > Following is my code : > > > hdLocMgr = (LocationManager) > > getSystemService(Context.LOCATION_SERVICE); > > Log.w("LocationTracker", "The Location Manager is : > > "+hdLocMgr.toString()); > > hdLocProvider = hdLocMgr.getBestProvider(hdCrit, true); > > location = hdLocMgr.getLastKnownLocation(hdLocProvider); > > dlat = location.getLatitude(); > > dlon = location.getLongitude(); > > Log.w("LocationTracker", "The value of Latitude is : > > "+dlat.toString()); > > > The code gives a RunTimeException (caused by NullPointerException as > > stated in it). > > Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine > LogCat and look at the stack trace to find the line that has the > NullPointerException. If I had to guess, your location variable is > null, because getLastKnownLocation() is null, because there is no > location for the provider, because you have not requested location > updates on that provider. > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy > > _Android Programming Tutorials_ Version 3.4 Available!- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en