springboot测试报错:null不能转换为int类型

本文详细解析了在Spring Boot应用中,使用Integer而非int的基本类型,如何避免空指针异常导致的转换错误。强调了基本类型与包装类在默认值、传值方式上的不同,以及在实际开发中选择合适类型的策略。

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

错误:
    There was an unexpected error (type=Bad Request, status=400).
    Failed to convert property value of type 'null' to required type 'int' for property 'age'; 
    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

原因:
    数据库对应的实体类,定义了int类型字段,null转换时报错

解决:int换为对应的包装类Integer

 

温习:基本类型和包装类区别
      默认值:
          包 装 类 Byte  Short Integer Long  Float  Double Character Boolean 默认值为null
          基本类型 byte-0 short-0 int-0 long-0 float-0.0 double-0.0 char-空格 boolean-false
      传 值:
           包装类是引用传递-传的不是本身,而是本身(对象)的引用(获取属性、值)地址(存在引用关系)
           基本类型是值传递-传的就是本身所代表的值,就传个值(结束)


           

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韧小钊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值