Skip to content

fix: NullPointerException Listener must not be null #357

@g4rb4g3

Description

@g4rb4g3

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @react-native-community/geolocation@3.4.0 for the project I'm working on.

We see some NullPointerExceptions caused by the fact that only the latest callback is stored in an instance variable when calling getCurrentPosition. So when doing multiple getCurrentPosition calls it is possible that the callback sets the instance variable to null even when other location requests might be resolved later on. There are not further null checks so removeLocationUpdates might be called with null leading to the crashes we see.
To prevent this I removed the instance variable and just pass this to removeLocationUpdates inside the LocationCallback so it is using the correct instance all the time and not have any null values.

Here is the diff that solved my problem:

diff --git a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java
index 423f7a5..97ae0d3 100644
--- a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java
+++ b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java
@@ -30,7 +30,6 @@ import com.google.android.gms.location.SettingsClient;
 public class PlayServicesLocationManager extends BaseLocationManager {
     private FusedLocationProviderClient mFusedLocationClient;
     private LocationCallback mLocationCallback;
-    private LocationCallback mSingleLocationCallback;
     private SettingsClient mLocationServicesSettingsClient;
 
     protected PlayServicesLocationManager(ReactApplicationContext reactContext) {
@@ -46,8 +45,8 @@ public class PlayServicesLocationManager extends BaseLocationManager {
         Activity currentActivity = mReactContext.getCurrentActivity();
 
         if (currentActivity == null) {
-            mSingleLocationCallback = createSingleLocationCallback(success, error);
-            checkLocationSettings(options, mSingleLocationCallback, error);
+            LocationCallback singleLocationCallback = createSingleLocationCallback(success, error);
+            checkLocationSettings(options, singleLocationCallback, error);
 			return;
         }
 
@@ -57,8 +56,8 @@ public class PlayServicesLocationManager extends BaseLocationManager {
                         if (location != null && (SystemClock.currentTimeMillis() - location.getTime()) < locationOptions.maximumAge) {
                             success.invoke(locationToMap(location));
                         } else {
-                            mSingleLocationCallback = createSingleLocationCallback(success, error);
-                            checkLocationSettings(options, mSingleLocationCallback, error);
+                            LocationCallback singleLocationCallback = createSingleLocationCallback(success, error);
+                            checkLocationSettings(options, singleLocationCallback, error);
                         }
                     });
         } catch (SecurityException e) {
@@ -167,8 +166,7 @@ public class PlayServicesLocationManager extends BaseLocationManager {
 
                 callbackHolder.success(location);
 
-                mFusedLocationClient.removeLocationUpdates(mSingleLocationCallback);
-                mSingleLocationCallback = null;
+                mFusedLocationClient.removeLocationUpdates(this);
             }
 
             @Override

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions