0

this is the first question I am posting on stack overflow. So if I have made any mistakes while posting the issue please feel free to tell me. I am developing an application using Mapbox's navigation SDK. I can show the map along with a driving route from source to destination but when I tap on the start navigation button the app is crashing.

Logcat showing->

**Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.services.android.navigation.ui.v5.NavigationView.onCreate(android.os.Bundle)' on a null object reference at com.mapbox.services.android.navigation.ui.v5.MapboxNavigationActivity.onCreate(MapboxNavigationActivity.java:32)

MyCode**

@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
    this.mapboxMap = mapboxMap;
    mapboxMap.setStyle(getString(R.string.navigation_guidance_day), style -> {

        enableMyLocationIfPermitted(style);

        button = findViewById(R.id.startButton);
        button.setOnClickListener(v -> {
            
                boolean simulateRoute = true;
                NavigationLauncherOptions options = NavigationLauncherOptions.builder()
                        .directionsRoute(currentRoute)
                        .shouldSimulateRoute(simulateRoute)
                        .build();

                NavigationLauncher.startNavigation(this, options);

        });
    });
}

MapboxNavigationActivity is a predefined class of Mapbox navigation SDK. By debugging the app, I found that the navigationView object is appearing null and this is the reason for the crash. I don't have an idea why it is null.

MapboxNavigationActivity line 32 debug screenshot

Younes
  • 462
  • 7
  • 15
  • I reopened this question because I don't think it is useful or helpful to close these questions as duplicates of the canonical "what is a null pointer exception" question. In this case, the NPE happens in code that was not written by OP and without much deeper understanding of how the frameworks (in this case Android and Mapbox) work, hasn't a chance to determine actually what he did wrong. Kudos to OP here, for being able to figure this one out. This type of problem occurs often and dev's usually need experienced assistance to solve this type of problem. – David Wasser Feb 06 '21 at 16:44
  • 1
    Thank you for reopening the question. Somewhere someone will make the same mistake that I did being unaware of what issue he/she will face because of some xml file name and they will waste so much of their time before finding the cause. I have searched many sites, links for the solution but unfortunately there is no answer for this anywhere. That's why I tried to explain this as clearly as I could. – Abhishek Nayak Feb 08 '21 at 07:20

1 Answers1

0

After wasting so much time I finally found the issue and solved it. My mistake was that I have an activity named as NavigationActivity whose xml name was activity_navigation. MapboxNavigationActivity which is the predefined class of Navigation SDK has also the same xml name i.e activity_navigation. This is the reason of conflict. At the time of operation, app was calling activity_navigation xml of NavigationActivity class which doesn't have a navigationView thus the crash.

So for future viewers if you are using Mapbox Navigation SDK then first check for any xml having the name of activity_navigation.

I am adding the image for clear understanding.

MapboxNavigationActivity onCreate methos