处理 Android Studio 的 NullPointerException lint 警告的正确方法 [英] Proper way to handle Android Studio's NullPointerException lint warning

查看:29
本文介绍了处理 Android Studio 的 NullPointerException lint 警告的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android/java 编程的新手,对如何正确处理此警告感到困惑.

I'm new to android/java programming and am confused how to properly deal with this warning.

方法调用''可能会产生'Java.lang.NullPointerException'

Method invocation '' may produce 'Java.lang.NullPointerException'

我应该使用 assert 来删除警告吗?

Should I be ussing assert to remove the warning?

或者更确切地说是运行时异常?

Or rather a runtime exception?

任何帮助将不胜感激.

推荐答案

我怀疑这个问题能否得到最终答案,因为这是一个见仁见智的问题.或者至少我相信 - 也是一种意见.:)

I doubt this question can be answered conclusively, as it's a matter of opinion. Or at least I believe so -- an opinion too. :)

我知道您想要0 个警告"(一个非常值得称赞的目标),但可能没有一刀切"的问题.话说……

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:

  • 使用断言.虽然您可以添加断言语句,但 Dalvik 会忽略它们.您可以根据需要配置模拟器以使用它们,但不能配置为真实设备(参见 我可以在 Android 设备上使用断言吗?).因此,虽然它可能会消除警告,但在实践中却毫无用处.
  • 让方法抛出NullPointerException.一般来说,这将是一个坏主意.在这种情况下,由于您可能覆盖 onOptionsItemSelected(),所以它甚至不可能.
  • Use assert. While you can add an assert statement, Dalvik ignores them. You can configure an emulator to use them if you want, but not a real device (see Can I use assert on Android devices?). So while it would possibly remove the warning, it's useless in practice.
  • Have the method throw NullPointerException. This would be a bad idea, generally. In this case, since you're probably overriding onOptionsItemSelected(), it's not even possible.

检查 (variable != null) 通常是最好的方法.但是,如果是,该怎么办,会提供一些其他选项.

Checking for (variable != null) is generally the best approach. What to do if it is, though, presents some other options.

  • 如果这是一个您可以恢复的问题,即即使 searchView 不存在,您也可以继续应用程序,只需这样做.例如,只是从方法返回.不过,最好记录这种情况,这样您就可以在测试时发现它.
  • 否则,如果无法继续,则抛出异常.你想尽早失败,这样问题就可以很容易被发现.这种情况下一个合理的例外是 IllegalStateException(参见 Java 等效于 .NET System.InvalidOperationException).它基本上表明此方法在不适当的时间执行.但请注意,作为 RuntimeException,这些异常未经过检查,因此可能会导致应用崩溃.
  • If it's a problem you can recover from, i.e. you can continue the application even though the 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.
  • Otherwise, if continuing isn't possible, throw an exception. You want to fail early, so that the problem can be easily detected. A reasonable exception for this case would be IllegalStateException (see Java equivalent to .NET System.InvalidOperationException). It basically indicates that this method was executed at an inappropriate time. Be careful though, that as a RuntimeException, these exceptions are unchecked, and hence will probably cause the app to crash.

这篇关于处理 Android Studio 的 NullPointerException lint 警告的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆