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

标签 constructor null findbugs

使用 findbugs 扫描以下代码后,它会报告 Dodgy code:NP: Load of known null value in new ....(at line where new Exception isthrowed)

有时需要在初始化对象之前检查 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;
  }

最佳答案

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

throw new EmpServiceException(aDepartment, "Invalid Request");

相当于
throw new EmpServiceException(null, "Invalid Request");

并希望您使用后者。是第一个参数 EmpServiceException@NonNull 注释的构造函数?

关于constructor - Findbugs 在验证构造函数参数时报告已知空值的负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167640/

相关文章:

c++ - 构造函数与析构函数环境组装

javascript - 为什么 JavaScript 有时会将 null 转换为空字符串,有时会转换为 "null"字符串?

sql-server - 使用 DBNull 或 null 插入/更新数据?

java - 在 FileReader 中发现对默认编码的依赖

java - 是否有开源插件可以根据 Findbugs Report 在 JIRA 中标记问题?

vb.net - 我可以将委托(delegate)从后代的构造函数传递到抽象类的构造函数吗?

继承中的 C++ 复制构造函数

java - 替代 FindBugs DefaultAnnotation for javax.annotation for fields and methods

C++构造函数继承(从派生类调用构造函数)

json - 在 json 或 javascript 中处理 null