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

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;
...

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (9 by maintainers)

Most upvoted comments

Any update on this? I can’t even use MapboxGL.MarkerView

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.

@realroutein Can you please describe what is different about this one?