LocationComponent com.mapbox.mapboxsdk.maps.MapboxMap.getLocationComponent()' on a null object reference

708 views Asked by At

Hey I'm doing a mapbox uber app. The rider and driver app used to work properly but for the past few days I'm getting a null error on the LocationCompoment ..can you guys help me to understand what is happening? Here is the code :

private void enableLocationComponent(@NonNull Style loadedMapStyle) {

        // Check if permissions are enabled and if not request
        if (PermissionsManager.areLocationPermissionsGranted(this)) {
    
            // Get an instance of the component
            LocationComponent locationComponent = mapboxMap.getLocationComponent();
                         if (locationComponent != null){
            // Activate with options
            locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this, loadedMapStyle).build());
    
            // Enable to make component visible
            locationComponent.setLocationComponentEnabled(true);
    
            // Set the component's camera mode
            locationComponent.setCameraMode(CameraMode.TRACKING);
    
            // Set the component's render mode
            locationComponent.setRenderMode(RenderMode.COMPASS);
       } } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
        }
    }

1

There are 1 answers

1
langsmith On BEST ANSWER

Why do you have two if (PermissionsManager.areLocationPermissionsGranted(this)) checks? You only need one.

Set up your project to match https://docs.mapbox.com/android/maps/examples/show-a-users-location/, which is the most basic way to show the device location.

You could also do a null check after LocationComponent locationComponent = mapboxMap.getLocationComponent();