startLocationUpdatesAsync causes NullPointerException on android

Please provide the following:

  1. SDK Version: 34
  2. Platforms(Android/iOS/web/all): Android installed apk

When in the code I do a startLocationUpdatesAsync sometimes when the application starts it reports to google a java.lang.NullPointerException:

java.lang.NullPointerException: 
  at expo.modules.location.taskConsumers.LocationTaskConsumer.shouldReportDeferredLocations (LocationTaskConsumer.java:316)
  at expo.modules.location.taskConsumers.LocationTaskConsumer.maybeReportDeferredLocations (LocationTaskConsumer.java:273)
  at expo.modules.location.taskConsumers.LocationTaskConsumer.access$100 (LocationTaskConsumer.java:44)
  at expo.modules.location.taskConsumers.LocationTaskConsumer$1.onComplete (LocationTaskConsumer.java:123)
  at com.google.android.gms.g.s.run (Unknown Source:4)
  at android.os.Handler.handleCallback (Handler.java:873)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:201)
  at android.app.ActivityThread.main (ActivityThread.java:6806)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)

The code that I have:

if (!await exports.isLocationDetectorEnabled()) return;
    if (!await Location.hasStartedLocationUpdatesAsync(TASK_BACKGROUND_LOCATION)) {
        await Location.startLocationUpdatesAsync(TASK_BACKGROUND_LOCATION, {
            accuracy: Location.Accuracy.Highest,
            showsBackgroundLocationIndicator: true,
            timeInterval: 5000, // only android
            distanceInterval: 200, // meters
            foregroundService: {
                notificationTitle: strings("LocationStickyNotification.title"),
                notificationBody: strings("LocationStickyNotification.body"),
                notificationColor: "#B398BF0E"
            },
        });
    }
if (!TaskManager.isTaskDefined(TASK_BACKGROUND_LOCATION)) {
    TaskManager.defineTask(TASK_BACKGROUND_LOCATION, ({ data, error }) => {
        if (error) {
            console.log("Error;" + error.message);
            return;
        }
        if (data) {
            const { locations } = data;
            var user = AsyncStorage.getItem('user').then(function(userStr) {
                user = JSON.parse(userStr);
                locations.forEach((location) => {
                    RestController.savePoints(user.userId, location.coords.latitude, location.coords.longitude, location.timestamp).then(function(){ });
                });
            });
        }
    });
}

It work’s on ios without problems but in android the first time that I open the app for first time it closes and when I reopen it again it works, why it crashes?

Do you get a stack trace when there is a NullPointerException? If so, could you post it?

won’t help for fully managed workflow but I’ve been using the following patch in expokit:

patch-package
--- a/node_modules/expo-location/android/src/main/java/expo/modules/location/services/LocationTaskService.java
+++ b/node_modules/expo-location/android/src/main/java/expo/modules/location/services/LocationTaskService.java
@@ -42,13 +42,17 @@ public class LocationTaskService extends Service {
   @Override
   @TargetApi(26)
   public int onStartCommand(Intent intent, int flags, int startId) {
-    Bundle extras = intent.getExtras();
+    if (intent != null) {
+      Bundle extras = intent.getExtras();
+
+      if (extras != null) {
+        mChannelId = extras.getString("appId") + ":" + extras.getString("taskName");
+      }
 
-    if (extras != null) {
-      mChannelId = extras.getString("appId") + ":" + extras.getString("taskName");
+      return START_STICKY;
     }
 
-    return START_STICKY;
+    return START_REDELIVER_INTENT;
   }
 
   public void setParentContext(Context context) {
@@ -100,7 +104,7 @@ public class LocationTaskService extends Service {
     }
 
     return builder.setCategory(Notification.CATEGORY_SERVICE)
-        .setSmallIcon(getApplicationInfo().icon)
+        .setSmallIcon(getResources().getIdentifier("notification_icon", "drawable", mParentContext.getPackageName()))
         .build();
   }
 
--- a/node_modules/expo-location/android/src/main/java/expo/modules/location/taskConsumers/GeofencingTaskConsumer.java
+++ b/node_modules/expo-location/android/src/main/java/expo/modules/location/taskConsumers/GeofencingTaskConsumer.java
@@ -116,6 +116,7 @@ public class GeofencingTaskConsumer extends TaskConsumer implements TaskConsumer
   @Override
   public boolean didExecuteJob(JobService jobService, JobParameters params) {
     List<PersistableBundle> data = getTaskManagerUtils().extractDataFromJobParams(params);
+    boolean didExecute = false;
 
     for (PersistableBundle item : data) {
       Bundle bundle = new Bundle();
@@ -125,9 +126,12 @@ public class GeofencingTaskConsumer extends TaskConsumer implements TaskConsumer
       bundle.putInt("eventType", item.getInt("eventType"));
       bundle.putBundle("region", region);
 
-      mTask.execute(bundle, null);
+      if (mTask != null) {
+        mTask.execute(bundle, null);
+        didExecute = true;
+      }
     }
-    return true;
+    return didExecute;
   }
 
   //endregion
--- a/node_modules/expo-location/android/src/main/java/expo/modules/location/taskConsumers/LocationTaskConsumer.java
+++ b/node_modules/expo-location/android/src/main/java/expo/modules/location/taskConsumers/LocationTaskConsumer.java
@@ -303,7 +303,7 @@ public class LocationTaskConsumer extends TaskConsumer implements TaskConsumerIn
   }
 
   private boolean shouldReportDeferredLocations() {
-    if (mDeferredLocations.size() == 0) {
+    if (mDeferredLocations.size() == 0 || mTask == null) {
       return false;
     }
     if (!mIsHostPaused) {

Maybe you could submit a pull request? :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.