You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
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
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.
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.
@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)
@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.
Good catch on the onSaveInstanceState()@tobrun@ljbade. 👍
To clarify about the throwing Exception, it's fine to have methods explicitly throw NullPointerExceptionBUT 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:
Activity
[android] #2801 - Log null centerCoordinate instead of throwing NullP…
[android] #2801 - Spelling fix for log statement
[android] #2801 - Log null centerCoordinate instead of throwing NullP…
[android] #2801 - Spelling fix for log statement
bleege commentedon Oct 26, 2015
Rebased and merged.
ljbade commentedon Oct 27, 2015
@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?
bleege commentedon Oct 27, 2015
@ljbade In cases like this we have 2 options:
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 containthrows Exceptionin it's method signature. This prevents calling code from knowing that it needs to call this with atry {} 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 putthrows Exceptionandtry {} catch {}all over the place was the better move.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.
tobrun commentedon Oct 27, 2015
@bleege
could you verify that you are overriding the correct
onSaveInstanceStateof 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)
tobrun commentedon Oct 27, 2015
@bleege just verified that you are not calling
onSaveInstanceStateWe need to adjust
onCreateof MapView to check if the bundle contains MapView keys.ljbade commentedon Oct 27, 2015
@tobrun @bleege Yeah that sounds like the root cause. You need to call
onSaveInstanceStateas 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.
ljbade commentedon Oct 27, 2015
Createed downstream ticket mapbox/mapbox-android-demo#38
bleege commentedon Oct 28, 2015
Good catch on the
onSaveInstanceState()@tobrun @ljbade. 👍To clarify about the throwing Exception, it's fine to have methods explicitly throw
NullPointerExceptionBUT they must havethrows NullPointerExceptionat the end of their method signature so that calling methods know totry catchthem 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:ljbade commentedon Oct 28, 2015
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.
[android] mapbox#2801 - Log null centerCoordinate instead of throwing…
[android] mapbox#2801 - Spelling fix for log statement