[英]Object is marked “javax.validation.constraints.NotNull” but is not initialized in this constructor
I have the following Object 我有以下对象
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
public class SomeObject {
@Valid
@NotNull
private int someCount;
public int getSomeCount() {
return someCount;
}
public void setSomeCount(final int someCount) {
this.someCount = someCount;
}
public SomeObject(final int someCount) {
this.someCount = someCount;
}
}
But I get 但是我明白了
"someCount" is marked "javax.validation.constraints.NotNull" but is not initialized in this constructor.
“ someCount”标记为“ javax.validation.constraints.NotNull”,但未在此构造方法中初始化。
and it's pointing to the constructor. 它指向构造函数。 I'm passing an item into the constructor.
我正在将一个项目传递给构造函数。 How do I fix this
我该如何解决
You are having a primitive type int
here. 您在此处具有基本类型
int
。 It can't be null. 不能为空。 Rather I would suggest you using
@Min(0)
validator instead. 相反,我建议您改用
@Min(0)
验证程序。 If you really need to have null passed in as a value then use a wrapper Integer
instead. 如果您确实需要将null作为值传递,请改用包装器
Integer
。 For that you can have a null check as above. 为此,您可以如上所述进行空检查。 I don't recommend this approach though.
我不推荐这种方法。
"javax.validation.constraints.NotNull" but is not initialized in this constructor “ javax.validation.constraints.NotNull”,但未在此构造方法中初始化
We have recently encountered this issue in our sonar build. 我们最近在声纳构建中遇到了这个问题。
To fix this issue we initialized the non-null field at the time of deceleration itself. 为了解决这个问题,我们在减速时初始化了非null字段。
Consider below code. 考虑下面的代码。
public class Dummy {
@NotNull(message = "Dummy field cannot be null")
private Integer dummyField = -1; // Any default value
public Dummy(Integer dummyField) {
this.dummyField = dummyField;
}
public Integer getDummyField() {
return dummyField;
}
public void setDummyField(Integer dummyField) {
this.dummyField = dummyField;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.