0

App crashes onstart with a null pointer exception. this happens when location permission is already granted. However doesnt occur when the location permission is not granted and only given after the app starts.

public void requestLocationUpdates() {
    Log.i(TAG, "Requesting location updates");
    Utils.setRequestingLocationUpdates(this, true);
    startService(new Intent(getApplicationContext(), 
LocationUpdatesService.class));
    try {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback, Looper.myLooper());
    } catch (SecurityException unlikely) {
        Utils.setRequestingLocationUpdates(this, false);
        Log.e(TAG, "Lost location permission. Could not request updates. " + 
unlikely);
    }
}

Below is the error.

 java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.projectu/com.example.projectu.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a 
null object reference
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual 
method 'void 
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a 
null object reference
    at com.example.projectu.MainActivity.onStart(MainActivity.java:265)
    at 
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
    at android.app.Activity.performStart(Activity.java:7029)
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
Mahesh CS
  • 9
  • 6

1 Answers1

0

you have not initialized mFusedLocationClient, and calling its mFusedLocationClient.requestLocationUpdates on null object

Neha Rathore
  • 429
  • 2
  • 9
  • i am using background service for location updates. it works when the permission is granted once the app has started. however when the permission is already provide prior to start the app crashes. – Mahesh CS Jun 18 '19 at 05:46
  • please refer this link. https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms/location/sample/locationupdatesforegroundservice – Mahesh CS Jun 18 '19 at 07:34
  • @Neha Did you solve the issue. If yes, can you please take a look at this.https://stackoverflow.com/questions/64459556/app-crashes-starting-locationupdatesservice-requestlocationupdates – Kumza Ion Oct 21 '20 at 11:11