Mapbox-navigation-android: Crash when testing reroute example

Created on 11 Oct 2017  路  16Comments  路  Source: mapbox/mapbox-navigation-android

Platform: Android 6.0 (Nexus 5)
Mapbox Navigation version: master branch

Steps to trigger behavior

  1. Open test app
  2. Go to Reroute example
  3. Click outside the route
  4. Start rerouting
  5. Click back button at the top
  6. App crashes

logcat

10-11 10:21:02.836 15054-15054/com.mapbox.services.android.navigation.testapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                Process: com.mapbox.services.android.navigation.testapp, PID: 15054
                                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'double com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress.distanceTraveled()' on a null object reference
                                                                                                    at com.mapbox.services.android.navigation.v5.navigation.NavigationMetricsWrapper.rerouteEvent(NavigationMetricsWrapper.java:110)
                                                                                                    at com.mapbox.services.android.navigation.v5.navigation.NavigationService$1.run(NavigationService.java:282)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Noting here that
https://github.com/mapbox/mapbox-navigation-android/blob/0fa13a0cf1618f8e0aba7566229b2d0c38c749b2/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationMetricsWrapper.java#L110-L112
may produce NullPointerException.

cc @cammace @ericrwolfe @zugaldia @electrostat

bug good first issue hacktoberfest

All 16 comments

I'll have a look at it and see if I can figure out where the null is coming from.

@sparkle-sparkle awesome! Go for it 馃挭 and let us know if you have any questions! Thanks 馃檹

I cannot start the Rerouting at all. I have the application up and running in a virtual Android device (API level 26, O). When I click/press on 'Reroute' the app crashes. Am I missing something in my setup? I use Android Studio.

In LogCat I see the following stacktrace:

10-11 18:08:51.994 2939-2939/com.mapbox.services.android.navigation.testapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                              Process: com.mapbox.services.android.navigation.testapp, PID: 2939
                                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mapbox.services.android.navigation.testapp/com.mapbox.services.android.navigation.testapp.activity.RerouteActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.mapbox.mapboxsdk.maps.MapView
                                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                               Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.mapbox.mapboxsdk.maps.MapView
                                                                                               Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.mapbox.mapboxsdk.maps.MapView
                                                                                               Caused by: java.lang.reflect.InvocationTargetException
                                                                                                  at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                                                  at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
                                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                                                                                                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                                  at com.mapbox.services.android.navigation.testapp.activity.RerouteActivity.onCreate(RerouteActivity.java:65)
                                                                                                  at android.app.Activity.performCreate(Activity.java:6975)
                                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                               Caused by: com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException: 
                                                                                              Using MapView requires setting a valid access token. Use Mapbox.getInstance(Context context, String accessToken) to provide one. 
                                                                                              Please see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one.
                                                                                              More information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens.
                                                                                                  at com.mapbox.mapboxsdk.Mapbox.validateAccessToken(Mapbox.java:101)
                                                                                                  at com.mapbox.mapboxsdk.Mapbox.getAccessToken(Mapbox.java:79)
                                                                                                  at com.mapbox.mapboxsdk.storage.FileSource.<init>(FileSource.java:124)
                                                                                                  at com.mapbox.mapboxsdk.storage.FileSource.getInstance(FileSource.java:51)
                                                                                                  at com.mapbox.mapboxsdk.maps.NativeMapView.<init>(NativeMapView.java:81)
                                                                                                  at com.mapbox.mapboxsdk.maps.MapView.initialise(MapView.java:115)
10-11 18:08:51.994 2939-2939/com.mapbox.services.android.navigation.testapp E/AndroidRuntime:     at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:81)
                                                                                                    ... 25 more

@sparkle-sparkle Thanks for digging into this one. Looks like you need to set up your Mapbox access token, look for a developer config file in the values. folder. You can get your (free) token from mapbox.com.

Thank you! That fixed it. I couldn't find where I needed to add my API token.

Steps taken:

  1. start the app
  2. choose Reroute
  3. click somewhere on the map (outside drawn route)

result:
After a delay I can see that de route is recalculated/redrawn. I do not see any NullPointerExceptions occurring.

I am trying to figure out where the different outcomes are coming from.
Are the described steps exactly as you executed them when the NPE occurred? Is there a difference perhaps in setup? Something else?

Hey @sparkle-sparkle thanks for reporting your findings.

As indicated in OP you should:

  1. Click back button at the top

and then sometimes the app crashes. The reason of this randomness it's because events are sent asynchronously and most of the times the resources are cleaned up properly but not always. Try it a couple of times and you'll hit the crash. BTW, the issue is that we're not checking nullability here:

https://github.com/mapbox/mapbox-navigation-android/blob/0fa13a0cf1618f8e0aba7566229b2d0c38c749b2/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationMetricsWrapper.java#L110-L112

Adding checks for those cases should do the trick.
Let us know if you have any other follow up questions. Thanks!

I have rerouted, following the previously mentioned steps and clicked the back button at the top of the screen and so far I haven't seen a NPE...

Is anyone working on this? For reference this is a duplicate of #395.

I cannot reproduce the NPE. I am currently not working on it anymore.

As @palto-ccadoret reported in #395 NPE could also happen in sessionState.routeProgressBeforeReroute().

@sparkle-sparkle
Thanks for trying to reproduce OP.

@Grsmto
Thanks for the heads up and if you want to tackle it, go for it because @sparkle-sparkle is not working on it.
Let us know if you have any follow up questions. Happy to help.

Thanks @Guardiola31337

Another stacktrace :
com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress.distanceTraveled()' on a null object reference at android.app.ActivityThread.handleStopService(ActivityThread.java:3344) at android.app.ActivityThread.-wrap27(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1570) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress.distanceTraveled()' on a null object reference at com.mapbox.services.android.navigation.v5.navigation.NavigationMetricsWrapper.rerouteEvent(NavigationMetricsWrapper.java:110) at com.mapbox.services.android.navigation.v5.navigation.NavigationService$1.run(NavigationService.java:282) at com.mapbox.services.android.navigation.v5.navigation.NavigationService.onDestroy(NavigationService.java:96) at android.app.ActivityThread.handleStopService(ActivityThread.java:3327) at android.app.ActivityThread.-wrap27(ActivityThread.java)聽 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1570)聽 at android.os.Handler.dispatchMessage(Handler.java:102)聽 at android.os.Looper.loop(Looper.java:154)聽 at android.app.ActivityThread.main(ActivityThread.java:6077)聽 at java.lang.reflect.Method.invoke(Native Method)聽 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)聽 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)聽

I use the version 0.6.2 of mapbox navigation.

@sparkle-sparkle, don't know why but a reroute occurs when ending the navigation.

If you start twice a navigation, a reroute seems to occur
and the route progress seems to be constucted asynchronously

Hey sorry, I didn't figure out how to fix this so I'm not about to submit a PR :/

Hey guys, I've also experienced the same NPE.

I notice that a null check is done on routeProgress before calling cancelEvent here:
https://github.com/mapbox/mapbox-navigation-android/blob/v0.6.3/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationService.java#L105

This means it is expected that routeProgress can be null at this point.

However, on the line right before this, a call to sendRerouteEvent is made, not inside the null check. And sendRerouteEvent calls NavigationMetricsWrapper.rerouteEvent(sessionState, routeProgress, sessionState.lastRerouteLocation()) which uses routeProgress and assumes it is not null.

A crash is therefore inevitable.

I would be grateful if someone from Mapbox could confirm this :)

Thanks for the feedback everyone - we are working to resolve the issues being found in NavigationMetricsWrapper this week for the next release. Keep an eye on the PRs moving and we will update this ticket when the fixes land. Thank you for your patience!

With #457, this issue should be resolved now, closing.

Was this page helpful?
0 / 5 - 0 ratings