1

I'm getting a null pointer exception from the following line of code:

    double longitude = location.getLongitude();

I having an issue figuring out what the problem is.

    LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, waypointActivity);

    //LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);

Here is the logcat output:

04-30 10:20:54.988: E/AndroidRuntime(1827): FATAL EXCEPTION: main
04-30 10:20:54.988: E/AndroidRuntime(1827): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.os.Looper.loop(Looper.java:137)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at dalvik.system.NativeStart.main(Native Method)
04-30 10:20:54.988: E/AndroidRuntime(1827): Caused by: java.lang.NullPointerException
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
04-30 10:20:54.988: E/AndroidRuntime(1827):     at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
04-30 10:20:54.988: E/AndroidRuntime(1827):     ... 11 more
04-30 10:21:04.048: E/Trace(1863): error opening trace file: No such file or directory (2)

I can't understand why it thinks my location variable is null. Any suggestions would be appreciated.

user268397
  • 1,917
  • 7
  • 38
  • 56
  • See http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null – Fildor Apr 30 '13 at 15:31
  • 1
    no one thinks your location variable is null. Apparently it has issues with loading your apk for some reason, possibly the name of your application ? unless some of your process is in a static block ? – njzk2 Apr 30 '13 at 15:31
  • where you are trying to get the longitude ?? it should be in `onLocationChanged()` method – Houcine Apr 30 '13 at 15:31
  • Did you set `` ? – Alexis C. Apr 30 '13 at 15:32
  • Yes I did include that permission – user268397 Apr 30 '13 at 15:33
  • @njzk2 you should add this as an answer. I totally missed that. – Fildor Apr 30 '13 at 15:35
  • Your logcat is contradicting your question... – DigCamara Apr 30 '13 at 15:36
  • The shown logcat is the one that is thrown when the app is being installed while still running on the device/emulator (should be only with ICS or above). The OP just pasted the wrong stacktrace. @OP please check below that for another stacktrace! – WarrenFaith Apr 30 '13 at 15:38

3 Answers3

5

The documentation states:

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

Since you're getting null for GPS location, you probably have not turned on your GPS or your phone has no GPS.

Change your code to

Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
   double longitude = location.getLongitude();
   double latitude = location.getLatitude();
   String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
}
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • it can be null even if the provider is enabled. Mostly if the location wasn't requested before before... – WarrenFaith Apr 30 '13 at 15:39
  • @WarrenFaith I imagine you're right, but I'm just stating what the docs say. Do you have a reference that confirms that behavior? – DigCamara Apr 30 '13 at 15:42
  • Nearly every time I start an app :) No real data, just my personal experience. – WarrenFaith Apr 30 '13 at 16:00
  • Heh. Yeah, I can confirm it on that level. However, I don't know if all devices will behave accordingly... – DigCamara Apr 30 '13 at 16:02
  • Even though my provider is enabled(I checked with isProviderEnabled() method), my location returns null so I can't get latitude and longitude. What can be the problem? – user3104760 May 17 '16 at 10:11
0

The LocationManager.GPS_PROVIDER(accessing location using phone's GPS) takes some time to access location, due to which you are getting location as null from locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)

Make your location object global and in your waypointActivity initialize this object in the implemented onLocationChanged method of LocationListener

Something like this:-

@Override
public void onLocationChanged(Location location) {
    this.location = location;
}

then check if the location is not null before using it as:-

    if (location!=null){
       double longitude = location.getLongitude();
       double latitude = location.getLatitude();
       String locLat = String.valueOf(latitude)+","+String.valueOf(longitude);
    }

I would recommend you use LocationManager.NETWORK_PROVIDER insted, because it gives the result much faster than GPS_PROVIDER.

bakriOnFire
  • 2,685
  • 1
  • 15
  • 27
0

This is not a "fix" but more a "workaround":

I have the same issue with my GPS location, though mine seems to be 'the first location returned after reinstalling the app via adb' which makes sense that the provider had not initialised yet. (as reinstalling the app removes any previous known location)

This results in whatever GPS task I was trying to accomplish always failing and returning null the first time it was triggered (all providers), but working as expected when restarted.

I was advised by my supervisor and college to simply workaround the issue by simply beginning gps updates and throwing away the first location, moving immediately onto the next and proceeding from there as normal (detecting and/or handling any null location returned from then onwards as unexpected behaviour)

While this serves the purpose for me, i'd still be interested in how this is actually supposed to be best-practice solved?

Hope this helps someone continue working in the meantime.

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
SSQ
  • 83
  • 8