mapbox / mapbox/mapbox-navigation-android
Passing a Route from v5.MapboxDirections into NavigationView Crashes
Nobody has claimed this yet.
- Dominant language
- Kotlin
- Stars
- 651
- Forks
- 321
- PR merge metrics
- No merged PRs in 30d
Description
**Mapbox Navigation SDK version:**
com.mapbox.mapboxsdk:mapbox-android-sdk:9.6.0 (And current master as of Feb 18th, 2021)
### Steps to trigger behavior
1. Create a route using 'com.mapbox.api.directions.v5.MapboxDirections.builder()'
2. Origin & Destination are: `29.0508283 -80.90549659999999` & `29.0485303934386 -80.90262127450215` (But I don't believe this matters, what matters is no waypoints)
3. The retrofit callback from the enqueueCall returns what appears to be a valid route which I assign the first one to `currentRoute`.
- I print out the legs() of the currentRoute, the output is this:
` [RouteLeg{distance=524.076, duration=71.744, durationTypical=null, summary=North Atlantic Avenue, Beachway Avenue, admins=[Admin{countryCode=US, countryCodeAlpha3=USA}], steps=[], incidents=null, annotation=null}]`
- The important thing to notice in this is `steps`
4. Call: this._mapbox.drawRoute(currentRoute); (or call `this._mapbox.startNavigation` using this route)
### Expected behavior
Works and doesn't crash
### Actual behavior
Same callstack as #3449
```
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine.buildWayPointFeatureFromLeg(MapRouteLine.java:359)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine.buildWayPointFeatureCollectionFrom(MapRouteLine.java:351)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine.drawWayPoints(MapRouteLine.java:345)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine.access$300(MapRouteLine.java:45)
at com.mapbox.services.android.navigation.ui.v5.route.MapRouteLine$1.onRouteFeaturesProcessed(MapRouteLine.java:331)
at com.mapbox.services.android.navigation.ui.v5.route.FeatureProcessingTask$1.run(FeatureProcessingTask.java:65)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
```
## #Reason Why
This code loops thru the legs, and works...
https://github.com/mapbox/mapbox-navigation-android/blob/e1f0918b6e73ceaf8bd3d1ef3886c8600289b184/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtils.kt#L463
Calls this function with `it = leg` and `0 = index`:
https://github.com/mapbox/mapbox-navigation-android/blob/e1f0918b6e73ceaf8bd3d1ef3886c8600289b184/libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtils.kt#L486
However, if you notice the above `steps` supplied by the MapBox directions API, it is an empty array; so the:
The key part is this: `leg.steps()?.get(index)?` lets dissect it...
`leg.steps()?` <- Exists and is NOT null (good so far), but then `.get(index)?` since there is no `0` item in the steps array the .get crashes with the error:
`java.lang.IndexOutOfBoundsException: Index: 0, Size: 0` as you are trying to get a element out of a zero sized array.
### Workaround
When using the MapBoxDirection Builder, pass `.steps(true)` into it so that it will create items in the steps array.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at MapRouteLine.buildWayPointFeatureFromLeg and MapRouteLineUtils.kt around the linked route-line processing calls; reproduce the crash with a route whose leg has an empty steps array and with MapboxDirections.steps(false). Trace how drawRoute or startNavigation processes that leg, then verify the route no longer crashes when steps are omitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100