gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:58:14 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com