gpt4 book ai didi

java - 管理流中的 null 不匹配

转载 作者:行者123 更新时间:2023-12-02 02:19:43 26 4
gpt4 key购买 nike

我尝试使用流来管理 dto 到 bean。

List<CarDto> carsDto = dto.getCars();
List<Cars> cars = bean.getCars();

for (Cars car : cars) {

if (carsDto==null || carsDto.stream().noneMatch(c -> c.getId().intValue() == car.getId())) {
bean.removeCars(car);
}

}

实际上,当 carsDto 的元素为空时,我得到空指针异常。

当元素为空时,我想做

bean.removeCars(car);

最佳答案

堆栈跟踪或一些有关类的代码会非常有帮助。我敢打赌,NPE 出现在包含 if 的行中,并且来自在以下两个之一中将 null 转换为原始 int 的情况:

c.getId()   // returns null or
car.getId() // returns null

如果 NPE 不是来自其中之一,则 dto.getCars() 可能会返回一个带有 null 值的 Listbean.removeCars(car) 抛出 NPE...

关于java - 管理流中的 null 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48687688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com