错误原因:使用HttpServletRequest包下的request对象进行request.getAttribute()和request.setAttribute()进行赋值取值时的空指针异常。

错误情况:

解决方法:通过添加if语句进行空值检查,可以避免NullPointerException的发生。

修改前代码:

for (Sale sale : sales) {
            sale.setProduct(productService.getProductByProductId(sale.getProductId()));
            sale.getProduct().setGroup(groupService.getGroupByGroupId(sale.getProduct().getGroupId()));
        }

修改后代码:

for (Sale sale : sales) {
            sale.setProduct(productService.getProductByProductId(sale.getProductId()));
            if (sale.getProduct() != null) {
                sale.getProduct().setGroup(groupService.getGroupByGroupId(sale.getProduct().getGroupId()));
            }
        }

成功解决问题!
Logo

鸿蒙生态一站式服务平台。

更多推荐