Findbugs报告在验证构造函数参数时加载已知的空值

rin*_*rer 4 null constructor findbugs

在使用findbugs扫描下面的代码后,它会报告Dodgy代码:NP:在新....中加载已知的空值(在抛出新异常的行)

有时需要在初始化对象之前检查null.为什么这被认为是"狡猾"?

public class Employee{

  @Valid
  private Department dept;

  @JsonCreator
  public Employee(@JsonProperty(value = "department", required = true) Department aDepartment)
      throws EmpServiceException{
    if (aDepartment == null) {
      throw new EmpServiceException(aDepartment, "Invalid Request");
    }
    this.dept= aDepartment;
  }
Run Code Online (Sandbox Code Playgroud)

Dav*_*ess 5

我的猜测是FindBugs指出你抛出异常的那一行

throw new EmpServiceException(aDepartment, "Invalid Request");
Run Code Online (Sandbox Code Playgroud)

相当于

throw new EmpServiceException(null, "Invalid Request");
Run Code Online (Sandbox Code Playgroud)

并希望你使用后者.是否用该EmpServiceException注释构造函数的第一个参数@NonNull