A null value cannot be assigned to a primitive type

今天使用jdbctemplate 查询数据封装到 实体里面时出现以下错误,记录一下。

错误信息:

org.springframework.beans.TypeMismatchException: 
Failed to convert property value of type 'null' to required type 'float' for property 'longitude’; 
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [float] for value 'null’; 
nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type

测试查询语句:

@Test
public void queryTest(){
    String sql = "select * from DIM_WDS_POINT limit 10 ";
    List<DimWdsPoint> dimWdsPoints = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<DimWdsPoint>(DimWdsPoint.class));
    dimWdsPoints.forEach(System.out::println);
}

实体类:DimWdsPoint.class 的 longitude 属性

/**
 * 经度
 */
private float longitude;

原因是 由于字段 longitude 在实体类中使用的是 float类型,但是数据库中查询出来的数据为null ,赋值的时候是不能把null 赋值给Java 基础类型的。

这里就有一个Java 的基础知识点:Java 基础类型不能为null。

解决办法:把实体映射的类型改为包装类型就OK。

private Float longitude;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Resolved [org.springframework.web.method.annotation.ModelAttributeMethodProcessor$1: org.springframework.validation.BeanPropertyBindingResult: 4 errors<EOL>Field error in object 'order' on field 'order_id': rejected value [null]; codes [typeMismatch.order.order_id,typeMismatch.order_id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.order_id,order_id]; arguments []; default message [order_id]]; 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]<EOL>Field error in object 'order' on field 'phone': rejected value [null]; codes [typeMismatch.order.phone,typeMismatch.phone,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.phone,phone]; arguments []; default message [phone]]; 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]<EOL>Field error in object 'order' on field 'buy': rejected value [null]; codes [typeMismatch.order.buy,typeMismatch.buy,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.buy,buy]; arguments []; default message [buy]]; 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]<EOL>Field error in object 'order' on field 'total': rejected value [null]; codes [typeMismatch.order.total,typeMismatch.total,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [order.total,total]; arguments []; default message [total]]; 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 excep
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值