java 在 Android Studio 中抑制潜在的 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31931812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppress potential NullPointerException in Android Studio
提问by minipif
This:
这:
@Nullable
Item[] mItems;
public Item getItem(int position) {
return mItems[position];
}
produces the warning:
产生警告:
Array access 'mItems[position]' may produce NullPointerException
I want to suppress this warning (I know that getItem()will not be called if mItemsis null).
我想取消这个警告(我知道getItem()如果mItems为空则不会被调用)。
I've tried using the following annotations:
我尝试使用以下注释:
@SuppressWarnings({"NullableProblems"})@SuppressWarnings({"null"})
@SuppressWarnings({"NullableProblems"})@SuppressWarnings({"null"})
as well as with the //noinspectionnotation, but they're all not working.
以及//noinspection符号,但它们都不起作用。
Using @SuppressWarnings({"all"})works, but it's obviously not what I'm looking for.
使用@SuppressWarnings({"all"})作品,但这显然不是我要找的。
Android Studio doesn't offer any suppression option when I hit alt+ enter, just the options to add a (useless) null check.
当我点击alt+ 时enter,Android Studio 不提供任何抑制选项,只是添加(无用)空检查的选项。
回答by zerobasedindex
This works for me, but not sure why AS would want to use constant conditions as the suppressor. I think it has something to do with the skip null check as it's a constant condition (i.e., it'll always not be null).
这对我有用,但不确定为什么 AS 要使用恒定条件作为抑制器。我认为它与跳过空检查有关,因为它是一个恒定条件(即,它永远不会为空)。
@Nullable
Item[] mItems;
@SuppressWarnings("ConstantConditions")
public Item getItem(int position) {
return mItems[position];
}
回答by sam
If you want to prevent Android Studio from bothering you with those warnings while keeping them in the compiler simply go to Settings -> Editor -> Inspections -> Constant Conditions and Exceptions and uncheck it.
如果您想防止 Android Studio 用这些警告打扰您,同时将它们保留在编译器中,只需转到设置 -> 编辑器 -> 检查 -> 常量条件和异常并取消选中它。
If instead you want to completely remove it then use the proper SuppressWarnings as suggested by the other answers :
相反,如果您想完全删除它,请按照其他答案的建议使用正确的 SuppressWarnings:
@SuppressWarnings("ConstantConditions")
回答by Hùng Ly
For me @SuppressWarningsdid not work. I just add assertstatement before the warnings to make it go away.
对我来说@SuppressWarnings没有用。我只是assert在警告之前添加声明以使其消失。
Your code should look like this:
您的代码应如下所示:
@Nullable
Item[] mItems;
public Item getItem(int position) {
assert mItems != null;
return mItems[position];
}
I know you said that you are sure the getItem()will not be called if mItemsis null. However, the inspector will not take into account that you already check before calling the function, hence the warning.
我知道你说过getItem()如果mItems是,你肯定不会被调用null。但是,检查器不会考虑您在调用函数之前已经检查过,因此会发出警告。
Or you can just leave it since mItems = nullwill never happen. But your code will look kind of messy, and for me it annoying.
或者你可以离开它,因为mItems = null永远不会发生。但是你的代码看起来有点乱,对我来说很烦人。
回答by Lukas Felber
@SuppressWarnings("ConstantConditions") does the trick for me.
@SuppressWarnings("ConstantConditions") 对我有用。


