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

错误:
    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
      传 值:
           包装类是引用传递-传的不是本身,而是本身(对象)的引用(获取属性、值)地址(存在引用关系)
           基本类型是值传递-传的就是本身所代表的值,就传个值(结束)


           

猜你喜欢

转载自blog.csdn.net/qq_36434219/article/details/84977399