Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Draw Marker, mNativeMapView NullPointerException #5106

Closed

Description

@BrunoDrean

Hello,

I integrated MapBox 4.0.1 in my mobile app, It's an navigation application like Waze for example.

I've a problem in a particular case : the user click on the back button (onBackPressed was called) when MapBox drawing a Markers on a Map.

The NullPointer Exception is generated here : mapboxMap.addMarker(op);

but looking closer the StrackTrace, we notice (MapView.java:988)

return mNativeMapView.addMarker(marker);

I Think that when the user "onBackPressed" the object "mNativeMapView" becomes "null"

here is the code in question :

mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(MapboxMap mapboxMap) {
            [..]
                        if (mapboxMap != null) {
                            MarkerOptions op = new MarkerOptions().position(...).title(...).icon(...);
                            mapboxMap.addMarker(op);                           
                        }
            [..]
            }           
}
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.mapbox.mapboxsdk.maps.NativeMapView.addMarker(com.mapbox.mapboxsdk.annotations.Marker)' on a null object reference
       at com.mapbox.mapboxsdk.maps.MapView.addMarker(MapView.java:988)
       at com.mapbox.mapboxsdk.maps.MapboxMap.addMarker(MapboxMap.java:591)
       at com.mapbox.mapboxsdk.maps.MapboxMap.addMarker(MapboxMap.java:574)
       at fr.agemos.labbe.android.controller.MapBoxActivity$5.onMapReady(MapBoxActivity.java:1370)
       at com.mapbox.mapboxsdk.maps.MapView.getMapAsync(MapView.java:2442)
       at fr.agemos.labbe.android.controller.MapBoxActivity.drawBridges(MapBoxActivity.java:1327)
       at fr.agemos.labbe.android.controller.MapBoxActivity.resultOverPassAsyncTask(MapBoxActivity.java:1913)
       at fr.agemos.labbe.android.service.asynctask.AsyncTaskOverPass.onPostExecute(AsyncTaskOverPass.java:161)
       at fr.agemos.labbe.android.service.asynctask.AsyncTaskOverPass.onPostExecute(AsyncTaskOverPass.java:29)
       at android.os.AsyncTask.finish(AsyncTask.java:632)
       at android.os.AsyncTask.access$600(AsyncTask.java:177)
       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6066)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

Thanks in advance for your help,

Activity

  1. added
    Android
    on May 23, 2016
  2. tobrun commented on May 25, 2016

    @tobrun
    Member

    @NonowPoney
    Thank you for reaching out and reporting your issue.

    The underlying code for addMarker is:

        long addMarker(@NonNull Marker marker) {
            return mNativeMapView.addMarker(marker);
        }

    It's not incorporating the check to validate if the underlying mNativeMapView has been destroyed:

    For example:

     long addPolyline(@NonNull Polyline polyline) {
            if (mDestroyed) {
                return 0l;
            }
            return mNativeMapView.addPolyline(polyline);
     }
  3. added a commit that references this issue on May 26, 2016
  4. tobrun commented on May 26, 2016

    @tobrun
    Member

    @bleege suggesting to put this on the 4.1.0 milestone. This solves the edge case of crashing if a marker is being added when the hosting Activity is being destroyed. Code can be viewed in 964a6cd and PR created in #5150.

  5. Schumi09 commented on May 27, 2016

    @Schumi09

    The same exception is thrown if you add a marker while the screen orientation changes.

  6. bleege commented on May 27, 2016

    @bleege
    Contributor

    @tobrun Sounds good to me. Let's redo the PR to get it onto the 4.1.0 Release Branch instead of master though.

  7. added this to the android-v4.1.0 milestone on May 27, 2016
  8. bleege commented on May 27, 2016

    @bleege
    Contributor

    @tobrun Actually with the time difference and it being Memorial Day weekend I'll just merge this fix into master now and then cherry pick it over to the release branch. This will make this fix available sooner for everyone. I'll take care of this now.

  9. added a commit that references this issue on May 27, 2016
  10. bleege commented on May 27, 2016

    @bleege
    Contributor

    Rebased and Merged to master

    screen shot 2016-05-27 at 11 44 29 am

  11. added a commit that references this issue on May 27, 2016
  12. bleege commented on May 27, 2016

    @bleege
    Contributor

    Cherry picked to release-android-v4.1.0. I'm going to start a new SNAPSHOT build so this (along with the other changes from today) will be available within an hour or so.

    screen shot 2016-05-27 at 11 46 49 am

  13. BrunoDrean commented on May 27, 2016

    @BrunoDrean
    Author

    Thank you for your help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Android

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions