不必要的 null 测试是什么意思?
What does unnecessary test for null mean?
通过以下方法,
public void setDescription(final String description) {
this.description = description;
if (this.description == null || this.description.isEmpty()) {
this.description = "[description]";
}
}
NetBeans 在 this.description == null
部分显示提示
Unnecessary test for null - the expression is never null
这是什么意思?我错过了什么?
更新
我有另一种情况具有相同(种类)的提示。
class User {
public void setUsername(final String username) {
if (this.username != null) { // <-- right here!
throw new IllegalStateException(
"already has a username");
}
this.username = username;
}
@NotNull
private String username;
}
NetBeans 为 this.username != null
部分显示了具有相同文本的提示。在这种情况下,NetBeans 监督 @NotNull
。如果您感兴趣,请跟踪以下问题。
您描述的行为与 Bug 226923 - Wrong warning 'the expression is never null' 相关。
该错误应在 netbeans 7.3.1 之前修复。我在 netbeans 8.0.2 中测试了您的代码,但没有出现任何提示。
通过以下方法,
public void setDescription(final String description) {
this.description = description;
if (this.description == null || this.description.isEmpty()) {
this.description = "[description]";
}
}
NetBeans 在 this.description == null
部分显示提示
Unnecessary test for null - the expression is never null
这是什么意思?我错过了什么?
更新
我有另一种情况具有相同(种类)的提示。
class User {
public void setUsername(final String username) {
if (this.username != null) { // <-- right here!
throw new IllegalStateException(
"already has a username");
}
this.username = username;
}
@NotNull
private String username;
}
NetBeans 为 this.username != null
部分显示了具有相同文本的提示。在这种情况下,NetBeans 监督 @NotNull
。如果您感兴趣,请跟踪以下问题。
您描述的行为与 Bug 226923 - Wrong warning 'the expression is never null' 相关。
该错误应在 netbeans 7.3.1 之前修复。我在 netbeans 8.0.2 中测试了您的代码,但没有出现任何提示。