I'm new to android/java programming and am confused how to properly deal with this warning.
Method invocation '' may produce 'Java.lang.NullPointerException'
Should I be ussing assert to remove the warning?
Or rather a runtime exception?
Any help would be appreciated.
I doubt this question can be answered conclusively, as it's a matter of opinion. Or at least I believe so -- an opinion too. :)
I understand you want "0 warnings" (a very laudable goal) but there's probably not a "one size fits all" issue. That said...
Things I believe you should not do:
NullPointerException
. This would be a bad idea, generally. In this case, since you're probably overriding onOptionsItemSelected()
, it's not even possible.Checking for (variable != null)
is generally the best approach. What to do if it is, though, presents some other options.
searchView
isn't there, just do so. For example, just return from the method. It's a good idea to log this situation though, so you can spot it while testing.RuntimeException
, these exceptions are unchecked, and hence will probably cause the app to crash.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With