Java:用于抑制空指针异常警告的方法注解

huangapple go评论84阅读模式
英文:

Java: Method annotation to suppress NullPointerException warning

问题

我在我的代码中有以下的辅助方法,用于断言关于类中成员字段的某些属性。

private @Nullable Controller mController;

private boolean isControllerReady() {
  return mController != null && mController.isReady();
}

在我的代码的其他地方,我调用类似这样的内容...

if (isControllerReady() && mController.canDoSomething()) {
  mController.doSomething();
}

我的Android Studio在canDoSomething()上给了我一个警告:

Method invocation 'canDoSomething' may produce 'NullPointerException'

这应该是一个误报。有没有可能对isControllerReady进行注解,以便IDE在canDoSomething上抑制/忽略这个NPE警告?

英文:

I have the following helper method in my code that asserts some property about a member field in my class.

private @Nullable Controller mController;

private boolean isControllerReady() {
  return mController != null && mController.isReady();
}

Elsewhere in my code, I invoke something like this...

if (isControllerReady() && mController.canDoSomething()) {
  mController.doSomething();
}

My Android Studio gives me a warning on canDoSomething():

Method invocation 'canDoSomething' may produce 'NullPointerException'

This should be a false positive. Is it possible to annotate isControllerReady so that the IDE suppresses/ignores this NPE warning on canDoSomething?

答案1

得分: 1

我认为@SuppressWarnings注解是你在寻找的。然后,你可以对方法进行注解,并传入你想要抑制的编译器警告(或警告)。

@SuppressWarnings({"NullableProblems"})
private boolean isControllerReady() {
  return mController != null && mController.isReady();
}

或者在Intellij中(这在Android Studio中也适用),你可以在突出显示的警告上按下Alt + Enter,然后从那里进行抑制。

参见https://www.jetbrains.com/help/idea/disabling-and-enabling-inspections.html?keymap=primary_xwin#suppress-inspections

编辑:格式化

英文:

I think the @SuppressWarnings annotation is what you're looking for. Then you'd annotate your method and pass in the compiler warning (or warnings) that you'd like to suppress.

@SuppressWarnings({"NullableProblems"})
private boolean isControllerReady() {
  return mController != null && mController.isReady();
}

Alternatively on Intellij (this should also work in Android Studio), you can press Alt + Enter on the highlighted warning and suppress it from there.

See https://www.jetbrains.com/help/idea/disabling-and-enabling-inspections.html?keymap=primary_xwin#suppress-inspections

Edit: formatting

答案2

得分: 1

由于您将mController标记为Nullable,编译器会尝试警告您在调用对象上的任何操作之前检查mController是否为非null。

您可以在条件语句中简单地添加空值检查。

if (isControllerReady() && mController != null && mController.canDoSomething()) {
  mController.doSomething();
}
英文:

As you annotated mController as a Nullable, the compiler tries to warn you to check if mController is not null before you invoke any operation on the object.

You can simply add a null check in the condition.

if (isControllerReady() && mController != null && mController.canDoSomething()) {
  mController.doSomething();
}

huangapple
  • 本文由 发表于 2020年10月8日 08:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64254317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定