简体   繁体   English

对象被标记为“ javax.validation.constraints.NotNull”,但未在此构造方法中初始化

[英]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.

相关问题 Lombok 的 @NonNull 在验证期间干扰 javax.validation.constraints.NotNull - Lombok's @NonNull interfering with javax.validation.constraints.NotNull during validation 处理SonarQube错误“ javax.validation.constraints.NotNull”的最佳方法是什么? - What is the best way to handle SonarQube error “javax.validation.constraints.NotNull” javax.validation.constraints.NotNull 不适用于 Kotlin 中的 java.time.Instant - javax.validation.constraints.NotNull not work for java.time.Instant in Kotlin Java 导入语句中的错误“无法解析导入 javax.validation.constraints.NotNull” - Error in Java Import statement "The import javax.validation.constraints.NotNull cannot be resolved" 标记为@NotNull的最终字段未初始化 - Final field marked @NotNull is not initialized javax验证不验证notNull - javax validation does not validate notNull Javax 约束:如果字段“类型”为 XXX,则应用 @NotNull 注释 - Javax Constraints: if field 'Type' is XXX then apply @NotNull annotation Javax @NotNull 注释用法 - Javax @NotNull annotation usage javax.validation.constraints中的转义序列无效 - Invalid escape sequencein javax.validation.constraints javax @notnull无效 - javax @notnull is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM