NullPointerException - Location & LocationManager - double

14,150

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);
}
Share:
14,150
user268397
Author by

user268397

Updated on June 04, 2022

Comments

  • user268397
    user268397 almost 2 years

    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.

    • Fildor
      Fildor about 11 years
    • njzk2
      njzk2 about 11 years
      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 ?
    • Houcine
      Houcine about 11 years
      where you are trying to get the longitude ?? it should be in onLocationChanged() method
    • Alexis C.
      Alexis C. about 11 years
      Did you set <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ?
    • user268397
      user268397 about 11 years
      Yes I did include that permission
    • Fildor
      Fildor about 11 years
      @njzk2 you should add this as an answer. I totally missed that.
    • DigCamara
      DigCamara about 11 years
      Your logcat is contradicting your question...
    • WarrenFaith
      WarrenFaith about 11 years
      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
    WarrenFaith about 11 years
    it can be null even if the provider is enabled. Mostly if the location wasn't requested before before...
  • DigCamara
    DigCamara about 11 years
    @WarrenFaith I imagine you're right, but I'm just stating what the docs say. Do you have a reference that confirms that behavior?
  • WarrenFaith
    WarrenFaith about 11 years
    Nearly every time I start an app :) No real data, just my personal experience.
  • DigCamara
    DigCamara about 11 years
    Heh. Yeah, I can confirm it on that level. However, I don't know if all devices will behave accordingly...
  • user3104760
    user3104760 almost 8 years
    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?