抑制Android Studio中潜在的NullPointerException [英] Suppress potential NullPointerException in Android Studio

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

问题描述

此:

@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"})
  • @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希望使用恒定条件作为抑制器.我认为这与skip null检查有关,因为它是一个恒定的条件(即,它永远不会为null).

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屋!

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