在 Android Studio 中抑制潜在的 NullPointerException [英] Suppress potential NullPointerException in Android Studio
问题描述
这个:
@Nullable
Item[] mItems;
public Item getItem(int position) {
return mItems[position];
}
产生警告:
Array access 'mItems[position]' may produce NullPointerException
我想取消此警告(我知道如果 mItems
为 null,则不会调用 getItem()
).
I want to suppress this warning (I know that getItem()
will not be called if mItems
is null).
我尝试使用以下注释:
@SuppressWarnings({"NullableProblems"})
@SuppressWarnings({"null"})
以及 //noinspection
符号,但它们都不起作用.
as well as with the //noinspection
notation, but they're all not working.
使用 @SuppressWarnings({"all"})
有效,但这显然不是我想要的.
Using @SuppressWarnings({"all"})
works, but it's obviously not what I'm looking for.
当我点击 alt + enter 时,Android Studio 不提供任何抑制选项,只是添加(无用的)空检查的选项.
Android Studio doesn't offer any suppression option when I hit alt + enter, just the options to add a (useless) null check.
推荐答案
这对我有用,但不确定为什么 AS 要使用常量条件作为抑制器.我认为这与跳过空检查有关,因为它是一个恒定条件(即,它永远不会为空).
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).
@Nullable
Item[] mItems;
@SuppressWarnings("ConstantConditions")
public Item getItem(int position) {
return mItems[position];
}
这篇关于在 Android Studio 中抑制潜在的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!