Mapbox-navigation-android: Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.maps.Style.getLayers()' on a null object reference

Created on 15 Aug 2019  路  8Comments  路  Source: mapbox/mapbox-navigation-android

Android API:6,7,8,9
*Mapbox Navigation SDK version:
'com.mapbox.mapboxsdk:mapbox-android-sdk:8.1.0'
'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.41.0'
*

Expected behavior

Should work fine on all devices

Actual behavior

App crashed with the following crash report:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.maps.Style.getLayers()' on a null object reference
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine.findRouteBelowLayerId + 301(MapRouteLine.java:301)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine. + 130(MapRouteLine.java:130)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute.buildMapRouteLine + 322(NavigationMapRoute.java:322)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute. + 139(NavigationMapRoute.java:139)
at com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute. + 118(NavigationMapRoute.java:118)
at com.tripmate.travelguidePakistan.AddMarkerAndNavigate$3.onResponse + 299(AddMarkerAndNavigate.java:299)
at com.mapbox.services.android.navigation.v5.navigation.NavigationRouteCallback.onResponse + 30(NavigationRouteCallback.java:30)
at com.mapbox.api.directions.v5.MapboxDirections$1.onResponse + 185(MapboxDirections.java:185)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run + 70(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback + 746(Handler.java:746)
at android.os.Handler.dispatchMessage + 95(Handler.java:95)
at android.os.Looper.loop + 148(Looper.java:148)
at android.app.ActivityThread.main + 5491(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 728(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main + 618(ZygoteInit.java:618)

bug

Most helpful comment

Are we good to close here then @LukasPaczos?

All 8 comments

Hey @haroonkhan9426 馃憢 thanks for reaching out and report your issue.

Could you add some more information around your test setup? Especially, could you share some code around how you're creating the NavigationMapRoute?

In any case, what are you trying to implement? Could you describe your use case in detail?

Have you 馃憖 the examples included in the app module? You can check DualNavigationMapActivity in which a NavigationMapRoute is created https://github.com/mapbox/mapbox-navigation-android/blob/f2a082cfd33792f2209c67979f615668d3f7a587/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/DualNavigationMapActivity.java#L315-L316

As you can see, in order to create a NavigationMapRoute the style needs to be loaded first https://github.com/mapbox/mapbox-navigation-android/blob/f2a082cfd33792f2209c67979f615668d3f7a587/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/DualNavigationMapActivity.java#L104-L107

If there is any sample or pseudo code you can provide, that would be really helpful and it'll give us better insight into your implementation and what could be causing your problem. Thanks!

E/Mbgl-MapChangeReceiver: Exception in onDidFinishLoadingStyle
java.lang.NullPointerException: Attempt to invoke virtual method 'com.mapbox.mapboxsdk.location.LocationComponent com.mapbox.mapboxsdk.maps.MapboxMap.getLocationComponent()' on a null object reference
at com.xcubesoftwares.mapbox.MainActivity.enableLocationComponent(MainActivity.java:101)
at com.xcubesoftwares.mapbox.MainActivity.access$000(MainActivity.java:63)
at com.xcubesoftwares.mapbox.MainActivity$2.onStyleLoaded(MainActivity.java:216)
at com.mapbox.mapboxsdk.maps.MapboxMap.notifyStyleLoaded(MapboxMap.java:889)
at com.mapbox.mapboxsdk.maps.MapboxMap.onFinishLoadingStyle(MapboxMap.java:213)
at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1286)
at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:198)
at com.mapbox.mapboxsdk.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:1069)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
A/libc: /usr/local/google/buildbot/src/android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type jni::PendingJavaException" failed
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 8749 (oftwares.mapbox)

this is my logcat

@Override
public void onPermissionResult(boolean granted) {

      if (granted) {

          Log.d("onPermissionResult", "permission granted");

          mapboxMap.getStyle(new Style.OnStyleLoaded() {
              @Override
              public void onStyleLoaded(@NonNull Style style) {
                  enableLocationComponent(style);

              }
          });

      } else {
          Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();

      }

  }

this is my java code

my app is creashed every time when i pree on allow to allowing user location permission

please provide me solution

@Amandeep-kaur11 looking at the stack trace, the exception does not come from Mapbox SDKs, you're not assigning the MapboxMap reference from OnMapReadyCallback to your mapboxMap class field:

com.mapbox.mapboxsdk.maps.MapboxMap.getLocationComponent()' on a null object reference
at com.xcubesoftwares.mapbox.MainActivity.enableLocationComponent(MainActivity.java:101)

Are we good to close here then @LukasPaczos?

I'm having the same problem too. It worked perfectly well with Java but when I switched to Kotlin it just wouldn't work and kept showing the below error message:

2020-11-03 15:09:17.889 7312-7312/com.siemens.j5_ips_android E/Mbgl-MapChangeReceiver: Exception in onWillStartLoadingMap
java.lang.NullPointerException: Attempt to invoke virtual method 'com.mapbox.mapboxsdk.style.layers.Layer com.mapbox.mapboxsdk.maps.Style.getLayer(java.lang.String)' on a null object reference
at com.mapxus.map.mapxusmap.k0.a(LocationLayer.java:73)
at com.mapxus.map.mapxusmap.k0.d(LocationLayer.java:3)
at com.mapxus.map.mapxusmap.location.LocationLayerPlugin.o(LocationLayerPlugin.java:6)
at com.mapxus.map.mapxusmap.location.LocationLayerPlugin.j(LocationLayerPlugin.java:1)
at com.mapxus.map.mapxusmap.location.LocationLayerPlugin$g.onWillStartLoadingMap(LocationLayerPlugin.java:1)
at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onWillStartLoadingMap(MapChangeReceiver.java:86)
at com.mapbox.mapboxsdk.maps.NativeMapView.onWillStartLoadingMap(NativeMapView.java:1050)
at com.mapbox.mapboxsdk.maps.NativeMapView.nativeSetStyleUrl(Native Method)
at com.mapbox.mapboxsdk.maps.NativeMapView.setStyleUri(NativeMapView.java:184)
at com.mapbox.mapboxsdk.maps.MapboxMap.setStyle(MapboxMap.java:939)
at com.mapbox.mapboxsdk.maps.MapboxMap.setStyle(MapboxMap.java:900)
at com.mapxus.map.mapxusmap.impl.MapboxMapViewProvider.a(MapboxMapViewProvider.java:23)
at com.mapxus.map.mapxusmap.impl.MapboxMapViewProvider.a(MapboxMapViewProvider.java:21)
at com.mapxus.map.mapxusmap.impl.MapboxMapViewProvider.setStyle(MapboxMapViewProvider.java:3)
at com.siemens.j5_ips_android.RoutePlanningOnFootActivity.changeStyle(RoutePlanningOnFootActivity.kt:505)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6892)
at android.widget.TextView.performClick(TextView.java:12693)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Oops, it's solved now haha

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phfeitosaa picture phfeitosaa  路  7Comments

Danny-James picture Danny-James  路  7Comments

hrach picture hrach  路  9Comments

ericrwolfe picture ericrwolfe  路  9Comments

Danny-James picture Danny-James  路  4Comments