A null value cannot be assigned to a primitive type
程序员文章站
2022-04-21 15:43:38
...
今天使用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;
上一篇: Optional int parameter 'xx' is present but cannot be translated into a null value
下一篇: jpa+springboot,数据查询异常check the manual that corresponds to your MySQL server version for the right ..
推荐阅读
-
JavaScript Uncaught TypeError: Cannot read property 'value' of null
-
The instance of entity type ‘xxx‘ cannot be tracked because another instance with the same key value
-
日期转化Json异常:JSON parse error: Cannot deserialize value of type `java.util.Date` from String
-
attempted to return null from a method with a primitive return type(int或double数字类型)
-
Null value was assigned to a property of primitive type setter of
-
JSON parse error: Cannot deserialize value of type `java.util.Date` from String
-
hibernate [Null value was assigned to a property of primitive type setter of sco
-
hibernate [Null value was assigned to a property of primitive type setter of sco
-
JavaScript Uncaught TypeError: Cannot read property 'value' of null
-
Optional long parameter 'id' is present but cannot be translated into a null value due to being decl