Maps: Exception in onDidFinishLoadingStyle Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.location.LocationComponent.setLocationComponentEnabled(boolean)' on a null object reference

Created on 11 May 2020  路  18Comments  路  Source: react-native-mapbox-gl/maps

Describe the bug
private vodid stateChanged() getting called before mLocationComponent is initiated on map load hence resulting in NullPointerException

2020-05-11 13:04:23.746 9008-9008/? E/Mbgl-MapChangeReceiver: Exception in onDidFinishLoadingStyle
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.location.LocationComponent.setLocationComponentEnabled(boolean)' on a null object reference
        at com.mapbox.rctmgl.components.location.LocationComponentManager.stateChanged(LocationComponentManager.java:80)
        at com.mapbox.rctmgl.components.location.LocationComponentManager.showUserLocation(LocationComponentManager.java:55)
        at com.mapbox.rctmgl.components.location.RCTMGLNativeUserLocation.onStyleLoaded(RCTMGLNativeUserLocation.java:55)
        at com.mapbox.mapboxsdk.maps.MapboxMap.getStyle(MapboxMap.java:119)
        at com.mapbox.rctmgl.components.location.RCTMGLNativeUserLocation.onMapReady(RCTMGLNativeUserLocation.java:43)
        at com.mapbox.mapboxsdk.maps.MapView.getMapAsync(MapView.java:1109)
        at com.mapbox.rctmgl.components.location.RCTMGLNativeUserLocation.addToMap(RCTMGLNativeUserLocation.java:30)
        at com.mapbox.rctmgl.components.mapview.RCTMGLMapView.addQueuedFeatures(RCTMGLMapView.java:526)
        at com.mapbox.rctmgl.components.mapview.RCTMGLMapView$1.onStyleLoaded(RCTMGLMapView.java:430)
        at com.mapbox.mapboxsdk.maps.MapboxMap.notifyStyleLoaded(MapboxMap.java:964)
        at com.mapbox.mapboxsdk.maps.MapboxMap.onFinishLoadingStyle(MapboxMap.java:221)
        at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1316)
        at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:198)
        at com.mapbox.mapboxsdk.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:1106)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:160)
        at android.app.ActivityThread.main(ActivityThread.java:6923)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
2020-05-11 13:04:23.751 9008-9312/? E/ReactNativeJS: 'Mapbox error', 'Exception in onDidFinishLoadingStyle', { level: 'error',
      tag: 'Mbgl-MapChangeReceiver',
      message: 'Exception in onDidFinishLoadingStyle' }

To Reproduce

Example:

<MapboxGL.MapView
        ref={map}
        style={[styles.container, props.style]}
        compassEnabled={false}
        onRegionIsChanging={onRegionIsChanging}
        rotateEnabled={props.rotationEnabled && !props.disableInteractions}
        onRegionDidChange={onRegionChanged}
        regionDidChangeDebounceTime={10}
        pitchEnabled={false}
        scrollEnabled={!props.disableInteractions}
        zoomEnabled={!props.disableInteractions}
        onLayout={props.onLayout}
        onPress={onPress}
      >
        <MapboxGL.Camera
          ref={camera}
          maxZoomLevel={18}
          defaultSettings={mapSettings}
          followUserLocation={props.keepUserCentered}
          followUserMode="normal"
        />
        {props.children}
        {/*
        //@ts-ignore*/}
        {props.showUserLocation && <MapboxGL.UserLocation showsUserHeadingIndicator renderMode="native" />}
      </MapboxGL.MapView>

Versions:

  • Platform: Android
  • Emulator/ Simulator: no
  • OS: Android 8
  • react-native-mapbox-gl 8.1.0-beta
  • React Native Version 61.2

Additional context
seems to be fixed by simple early return:

private void stateChanged() {
        if (mLocationComponent == null) return;
...
Android bug help wanted

Most helpful comment

I also agree to reopen. I have this issue with version 8.1.0-rc10 .

I get this issue when I have renderMode="native" on the <MapboxGL.UserLocation /> when the <Camera /> does not have followUserLocation prop (if followUserLocation is set to true it works)

All 18 comments

@nasmuris Thanks much for the report!

Can you please make a PR, with the suggested early return?! There are also some other methods that there that might use the same treatment

@mfazekas Sorry for misleading suggestion, I've discovered I've been updating the locationComponent once again after initial load manually by setting setting followUserLocation to true, hence it worked for me, so we need to find some other way to fix this bug, will try to give it some more time later in upcoming days

This is kind of blocking for me as well. The problem does not occur with renderMode="normal"

Maybe it's somehing there:
https://github.com/react-native-mapbox-gl/maps/blob/master/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/location/RCTMGLNativeUserLocation.java#L61

    @SuppressLint("MissingPermission")
    @Override
    public void onStyleLoaded(@NonNull Style style) {
        Context context = getContext();
        if (!PermissionsManager.areLocationPermissionsGranted(context)) {
            return;
        }

        LocationComponentManager locationComponent = mMapView.getLocationComponentManager();
        locationComponent.showUserLocation(mEnabled);
    }

    public void setRenderMode(@RenderMode.Mode int renderMode) {
        LocationComponentManager locationComponent = mMapView.getLocationComponentManager();
        locationComponent.setRenderMode(renderMode);
    }

I tried copying the PermissionsManager-Lines to the other method to have that early return. But it didn't work. I have no idea what I'm doing, hopefully someone else is smarter than me.

Just started to examine the issue and I'm having troubles with the LocationComponentManager being updated from both RCTMGLNativeUserLocation and RCTMGLCamera
from my point of view the userLocation related props (followUserLocation, followUserMode, followZoomLevel, followPitch, followHeading) should be part of UserLocation component, not the Camera itself as I can see those components/props interfere with each other

@nasmuris This could also be the source of many issues I have. So does the followPitch and followZoomLevel only sometimes apply (kind of a race condition, I guess). Also observing and setting the trackingMode via onUserTrackingModeChange could lead in an infinity loop of resetting the trackingModes.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I can confirm that this is still an issue on today's master. As far I can see mMapView is null in
https://github.com/react-native-mapbox-gl/maps/blob/452ce86355a6993e96617cf799b98876668b79e7/android/rctmgl/src/main/java/com/mapbox/rctmgl/components/location/RCTMGLNativeUserLocation.java#L59

I tried to simply add a null check, but that leads to similar issue another place. It seems that the order how mapView object is passed by is wrong. Any suggestions how to proceed in finding a bug?

for me it occurs when adding
implementation 'com.google.android.gms:play-services-location:17.0.0'

I added this to gradle because of device location being turned ON programmatically without going the user to settings menu then turn On the location.

I just got this crash too, exact same log. Could this be re-opened?

I just got this crash too, exact same log. Could this be re-opened?

I got this crash as well whenever i add an Annotation into a MapView

This crash does occur for us when rendering native mode on Android.

I also just had this crash occur with nativeRendering enabled for on Android. Can we re-open this issue?

I also agree to reopen. I have this issue with version 8.1.0-rc10 .

I get this issue when I have renderMode="native" on the <MapboxGL.UserLocation /> when the <Camera /> does not have followUserLocation prop (if followUserLocation is set to true it works)

A workaround by just not using renderMode="native" on Android. It works fine on iOS, so it's worth writing a ternary (conditional) operator distinguishing by Platform.OS so that the correct component is chosen. This should work for 8.1.0-rc10 as well.

This is still happening as per original issue description. Tested with

  • Simulator and Android 10
  • @react-native-mapbox-gl/maps": "8.1.0"

renderMode="native" on MapboxGL.UserLocation

I also agree to reopen. I have this issue with version 8.1.0-rc10 .

I get this issue when I have renderMode="native" on the <MapboxGL.UserLocation /> when the <Camera /> does not have followUserLocation prop (if followUserLocation is set to true it works)

Leaving followUserLocation={true} has the side effect of not being able to use Camera.moveTo(coordinates, duration) anymore, which I required.

Good new is, it is possible to set followUserLocation back to false during runtime (I used MapView.onDidFinishLoadingStyle() for this). This was safe to do, the error only seemed to happen during initialisation.

@mfazekas Would it be worth reopening this issue?

I have the same issue on Andorod with renderMode='native'.
It should be open unless there is no solution found.

Also hit this bug :-|. This issue seems to go away with 8.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RichardLindhout picture RichardLindhout  路  4Comments

dorthwein picture dorthwein  路  3Comments

atomheartother picture atomheartother  路  3Comments

SethArchambault picture SethArchambault  路  3Comments

alexisrougnant picture alexisrougnant  路  3Comments