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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章