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.

Don't Crash App If setCenterCoordinate() Is Null #2801

Closed

Description

@bleege

Getting crash reports from the Mapbox Demo App that are likely to also affect customer apps too. ;-) Instead of throwing a NullPointerException as it currently does, it should do a Null Check and just return with no impact if centerCoordinate is null.

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mapbox.mapboxandroiddemo/com.mapbox.mapboxandroiddemo.MainActivity}: java.lang.NullPointerException: centerCoordinate is null
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.access$800(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Caused by java.lang.NullPointerException: centerCoordinate is null
       at com.mapbox.mapboxsdk.views.MapView.setCenterCoordinate(MapView.java)
       at com.mapbox.mapboxsdk.views.MapView.setCenterCoordinate(MapView.java)
       at com.mapbox.mapboxsdk.views.MapView.onCreate(MapView.java)
       at com.mapbox.mapboxandroiddemo.MainActivity.onCreate(MainActivity.java)
       at android.app.Activity.performCreate(Activity.java)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
       at android.app.ActivityThread.access$800(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

Activity

  1. added
    Android
    on Oct 26, 2015
  2. added this to the android-v2.2.0 milestone on Oct 26, 2015
  3. added a commit that references this issue on Oct 26, 2015
  4. added 3 commits that reference this issue on Oct 26, 2015
  5. bleege commented on Oct 26, 2015

    @bleege
    ContributorAuthor

    Rebased and merged.

  6. ljbade commented on Oct 27, 2015

    @ljbade
    Contributor

    @bleege What about some of the other functions we also check for null? Should we implement a similar no-op?

    Also, why was the demo app trying to set a null coordinate in the first place?

  7. bleege commented on Oct 27, 2015

    @bleege
    ContributorAuthor

    @ljbade In cases like this we have 2 options:

    1. No-op and log
    2. Throw Exception

    The difference was the way this was originally implemented it was a runtime exception (note: not a RuntimeException) that crashed the app because the the setCenterCoordinate() method did not contain throws Exception in it's method signature. This prevents calling code from knowing that it needs to call this with a try {} catch {} block to handle exceptions and keep the app running. Since this type of method is more of an effect type I decided that going the No-op route vs refactoring to put throws Exception and try {} catch {} all over the place was the better move.

    Also, why was the demo app trying to set a null coordinate in the first place?

    Good question, but according to the crash reports it was happening on many different devices and versions of Android. Either way crashing the app on method call like this is not good.

  8. tobrun commented on Oct 27, 2015

    @tobrun
    Member

    @bleege
    could you verify that you are overriding the correct onSaveInstanceState of Activity?
    I have made that mistake and was given me the same exception as this issue.
    Maybe I will add MapActivity later to the SDK as documented in (#2616)

  9. tobrun commented on Oct 27, 2015

    @tobrun
    Member

    @bleege just verified that you are not calling onSaveInstanceState
    We need to adjust onCreate of MapView to check if the bundle contains MapView keys.

  10. ljbade commented on Oct 27, 2015

    @ljbade
    Contributor

    @tobrun @bleege Yeah that sounds like the root cause. You need to call onSaveInstanceState as explicitly written in the Javadoc. Google Maps API has same thing but perhaps they check for missing instance state... You will find the app has a bug where rotating the screen loses the current map position (since that is done via save instance state)

    The reason I added a null exception throw was that @bleege said to do it when we have @nonnull annotation. I note the Android SDK does not check for nulls at all, it just lets it crash at any code that tries to use the null object.

    Perhaps I will replace all of these with warning logs.

    I note that Android Studio also now detects many MapView functions with a code inspection warning about unhandled null exceptions, even though we don't use Throws.

  11. ljbade commented on Oct 27, 2015

    @ljbade
    Contributor

    Createed downstream ticket mapbox/mapbox-android-demo#38

  12. bleege commented on Oct 28, 2015

    @bleege
    ContributorAuthor

    Good catch on the onSaveInstanceState() @tobrun @ljbade. 👍

    To clarify about the throwing Exception, it's fine to have methods explicitly throw NullPointerException BUT they must have throws NullPointerException at the end of their method signature so that calling methods know to try catch them and can recover without crashing the app. We want to promote a stable SDK wherever possible. In this case (had I gone the throw route) it'd looked like this:

    // Original
    public void setCenterCoordinate(@NonNull LatLng centerCoordinate, boolean animated) {
    ...
    }
    
    // Change
    public void setCenterCoordinate(@NonNull LatLng centerCoordinate, boolean animated) throws NullPointerException {
    ...
    }
  13. ljbade commented on Oct 28, 2015

    @ljbade
    Contributor

    Yeah I think we want avoid the developer friction of Throws. Having to wrap every single bit of code in your app with try/catch gets tedious fast.

  14. added 2 commits that reference this issue on Nov 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Androidcrash

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions