Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 在Spring boot中使用MongoTemplate时出现NullPointerException_Mongodb_Spring Boot_Nullpointerexception_Spring Mongo - Fatal编程技术网

Mongodb 在Spring boot中使用MongoTemplate时出现NullPointerException

Mongodb 在Spring boot中使用MongoTemplate时出现NullPointerException,mongodb,spring-boot,nullpointerexception,spring-mongo,Mongodb,Spring Boot,Nullpointerexception,Spring Mongo,我有一个名为“statusbynumbers”的集合,我对实体的定义如下: @Document(collection="statusbynumbers") @Data @AllArgsConstructor @NoArgsConstructor public class StatusByNumbersEntity { @Id private String id; @Field("number") private String number; @Fie

我有一个名为“statusbynumbers”的集合,我对实体的定义如下:

@Document(collection="statusbynumbers") 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StatusByNumbersEntity {
    @Id
    private String id;

    @Field("number")
    private String number;

    @Field("level")
    private String 

    @Field("yat")
    private double yat;
}
“yat”键是数据库中的双精度类型。但在我的业务逻辑中,在某些情况下,“yat”可以为空。然后,当我使用MongoTemplate.find()方法时,导致了java.lang.NullPointerException:null。我的代码中使用的MongoTemplate.find()如下所示:

public class ReportsServiceImpl implements ReportsService {
    @Resource
    private MongoTemplate ReportsMongoTemplate;

    @Override
    public List<StatusByNumbersEntity> getNumbersByFilterLevel(String filterLevel) {
        List<Integer> filterLevelList = FilterLevelModel.buildFilterLevel(filterLevel);

        Criteria criteria = Criteria.where("level").in(filterLevelList);
        Query query = new Query(criteria);
        // java.lang.NullPointerException: null caused by the following code
        List<StatusByNumbersEntity> numList = ReportsMongoTemplate.find(query,StatusByNumbersEntity.class); 

    ...
}

即使键“yat”可以是null而不是double-type,如何使用MongoTemplate.find()方法来避免此NullPointerException

这是因为您对可空字段使用了一个基本类型变量(double,小写字母为'd')。 不能为基元数据类型设置空值(trows NullPointerException)

将“double”替换为“double”

@Field("yat")
private Double yat;
请记住,您也不能将null值强制转换为基本数据类型。 因为当您有setter和getter时,它们中的基元类型也会替换为非基元等效类型

更多详情:

这是因为您对可空字段使用了一个基本类型变量(double加小写字母“d”)。 不能为基元数据类型设置空值(trows NullPointerException)

将“double”替换为“double”

@Field("yat")
private Double yat;
请记住,您也不能将null值强制转换为基本数据类型。 因为当您有setter和getter时,它们中的基元类型也会替换为非基元等效类型

更多详情:

您的响应数据可能返回无法转换为双精度(yat)的数据。您的响应数据可能返回无法转换为双精度(yat)的数据