Findbugs`NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE`

时间:2017-04-10 19:47:17

标签: java jenkins-plugins findbugs

虽然尝试发布jenkins插件maven报告了一些findbugs错误,但我无法弄清楚如何修复。

""

有问题的行包含以下函数:

[INFO] Possible null pointer dereference in com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl.doFillAuthTokenCredentialIdItems() due to return value of called method [com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl, com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl] Dereferenced at VaultBuildWrapper.java:[line 326]Known null at VaultBuildWrapper.java:[line 326] NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
[INFO] Possible null pointer dereference in com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl.doFillAuthTokenCredentialIdItems() due to return value of called method [com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl, com.datapipe.jenkins.vault.VaultBuildWrapper$DescriptorImpl] Method invoked at VaultBuildWrapper.java:[line 329]Known null at VaultBuildWrapper.java:[line 329] NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

我尝试添加以下空检查无效:

public ListBoxModel doFillAuthTokenCredentialIdItems(){
  if (!Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER)) {
    return new ListBoxModel();
  }
  AbstractIdCredentialsListBoxModel model = new StandardListBoxModel().includeEmptyValue().includeAs(ACL.SYSTEM, Jenkins.getInstance(), VaultTokenCredential.class);
  return model;
}

提前致谢

1 个答案:

答案 0 :(得分:0)

不确定原因,但在返回之前创建新的ListBoxModel就可以了。

public ListBoxModel doFillAuthTokenCredentialIdItems() {
  final ListBoxModel lbm = new ListBoxModel();
  final Jenkins jenkins = Jenkins.getInstance();

  if (jenkins == null) {
    return lbm;
  }

  if (!jenkins.hasPermission(Jenkins.ADMINISTER)) {
    return lbm;
  }
  AbstractIdCredentialsListBoxModel model = new StandardListBoxModel().includeEmptyValue().includeAs(ACL.SYSTEM, jenkins, VaultTokenCredential.class);
  return model;
}