Field error in object 'books' on field 'bookId': rejected value [null]; codes [typeMismatch.books.bo

博客讲述了在使用Spring时遇到的一个验证错误,具体为Field error in object 'books' on field 'bookId',错误信息显示尝试将null值转换为int类型的bookId失败。作者尝试将books的参数重写为bookID后解决了问题,但当再次改回bookId时,错误未再现,怀疑可能是IDEA的bug。文中提供了Books类的部分代码,并表达了对问题原因的疑惑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误信息

01-May-2020 20:43:35.866 WARNING [http-nio-555-exec-1] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘books’ on field ‘bookId’: rejected value [null]; codes [typeMismatch.books.bookId,typeMismatch.bookId,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [books.bookId,bookId]; arguments []; default message [bookId]]; default message [Failed to convert value of type ‘null’ to required type ‘int’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [int] for value ‘null’; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type]]

解决方法
底层的books的参数重写为bookID
PS: 但是当我重写回bookId的时候又没有问题了,怀疑是idea有bug

在这里插入图片描述
下面是Books类的代码,一开始bookId报错,重写成bookID后不报错了,但是再次重写成bookId不报错.

package com.psj.pojo;

public class Books {
    private int bookId;
    private String bookName;
    private int bookCounts;
    private String detail;

    public Books() {
    }


    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(int bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }


    public Books(int bookId, String bookName, int bookCounts, String detail) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookCounts = bookCounts;
        this.detail = detail;
    }
    @Override
    public String toString() {
        return "Books{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookCounts=" + bookCounts +
                ", detail='" + detail + '\'' +
                '}';
    }
}

疑问

虽然现在没有报错了,但是还是找不出原因

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值