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

Draw Marker, mNativeMapView NullPointerException #5106

Closed
BrunoDrean opened this issue May 23, 2016 · 8 comments
Closed

Draw Marker, mNativeMapView NullPointerException #5106

BrunoDrean opened this issue May 23, 2016 · 8 comments
Labels
Android Mapbox Maps SDK for Android

Comments

@BrunoDrean
Copy link

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,

@tobrun tobrun added the Android Mapbox Maps SDK for Android label May 23, 2016
@tobrun
Copy link
Member

tobrun commented May 25, 2016

@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);
 }

@tobrun
Copy link
Member

tobrun commented May 26, 2016

@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.

@Schumi09
Copy link

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

@bleege
Copy link
Contributor

bleege commented May 27, 2016

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

@bleege bleege added this to the android-v4.1.0 milestone May 27, 2016
@bleege
Copy link
Contributor

bleege commented May 27, 2016

@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.

@bleege
Copy link
Contributor

bleege commented May 27, 2016

Rebased and Merged to master

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

@bleege
Copy link
Contributor

bleege commented May 27, 2016

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

@bleege bleege closed this as completed May 27, 2016
@BrunoDrean
Copy link
Author

Thank you for your help !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants