eclipse问题:潜在的零点分析

时间:2015-05-17 01:08:42

标签: eclipse intellij-idea nullable notnull

可能是,这已经多次讨论了。但是,我不清楚@javax.annotation.nullable和notnull在Eclipse中是如何工作的。我在eclipse中有以下代码。

if(null != getApplicant()){
  name = getApplicant().getName(); //Potential null pointer access
}

其中,getApplication()使用@Nullable注释。

在上面的代码中,我在访问之前验证了null。但是,我仍然在编译器中遇到错误。相同的代码工作正常Intellij。 (我团队中的大多数开发人员使用它:()。

请告诉我如何才能得到这个@ javax.annotation.Nullable和@ javax.annotation.Notnull在eclipse中以类似于Intellij的方式工作。我不想仅仅因为这种不同的行为而改变我的IDE。

2 个答案:

答案 0 :(得分:2)

编译器无法保证第二次调用getApplication()会像第一次那样返回非空结果。将其存储在本地进行测试,然后在if块中使用。

答案 1 :(得分:1)

Eclipse警告你,第二个方法访问可能会返回null。重现nullpointer的示例代码:

public class A {
int numberCalls;
Applicant getApplicant() {
if (numberCalls++ > 3)
  return null;
else
  return new Applicant();
}
}

我建议像这样实施你的电话:

Applicant applicant = getApplicant();
if(null != applicant){
  name = applicant.getName(); //not anymore Potential null pointer access
}