1

I'm receiving a null pointer exception on this line:

double latitude = location.getLatitude();

Is there anything I need to be doing to initialize my Location variable? What am I doing wrong?

Here is my source code:

    Location location;

    if (isGpsEnabled) { 
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    else { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    Log.i(TAG, "Provider is: "+provider);
    Log.i(TAG, "Location is: "+location);


    //Zooms into the current location when the activity is started
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

Here is the logcat output I'm receiving:

06-17 09:22:40.797: E/AndroidRuntime(12436): FATAL EXCEPTION: main
06-17 09:22:40.797: E/AndroidRuntime(12436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.polaris.epicriders.Rides.RideTracking}: java.lang.NullPointerException
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.os.Looper.loop(Looper.java:137)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at java.lang.reflect.Method.invoke(Method.java:511)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at dalvik.system.NativeStart.main(Native Method)
06-17 09:22:40.797: E/AndroidRuntime(12436): Caused by: java.lang.NullPointerException
06-17 09:22:40.797: E/AndroidRuntime(12436):    at com.polaris.epicriders.Rides.RideTracking.onCreate(RideTracking.java:273)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.Activity.performCreate(Activity.java:5104)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-17 09:22:40.797: E/AndroidRuntime(12436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-17 09:22:40.797: E/AndroidRuntime(12436):    ... 11 more
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
user268397
  • 1,917
  • 7
  • 38
  • 56

2 Answers2

2

getLastKnownLocation returns null if any Location has been acquired

from the doc

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.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Ok, do this interesting test. Launch google maps and check. If your app works all of a sudden, google maps did something to trigger a update on the lastKnownLocation cache inside the android OS running on the phone.

I did the following and it had the same result as kickstart that google maps would do. Try it out.

    HomeScreen.getLocationManager().requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @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 onLocationChanged(final Location location) {
        }
    });

Notice, I do nothing in the calls instead the listener. You just need to call your getLastKnownLocation after this kickstart.

OR

Try the LocationClient api's, they are new.

Siddharth
  • 9,349
  • 16
  • 86
  • 148