zoukankan      html  css  js  c++  java
  • 空指针异常

    问题

    CategoryVO类,

    package com.imooc.mall.model.vo;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    public class CategoryVO implements Serializable {
    
        private Integer id;
    
        private String name;
    
        private Integer type;
    
        private Integer parentId;
    
        private Integer orderNum;
    
        private Date createTime;
    
        private Date updateTime;
    
        private List<CategoryVO> childCategory = new ArrayList<>();
    
        public List<CategoryVO> getChildCategory() {
            return childCategory;
        }
    
        public void setChildCategory(List<CategoryVO> childCategory) {
            this.childCategory = childCategory;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Integer getType() {
            return type;
        }
    
        public void setType(Integer type) {
            this.type = type;
        }
    
        public Integer getParentId() {
            return parentId;
        }
    
        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }
    
        public Integer getOrderNum() {
            return orderNum;
        }
    
        public void setOrderNum(Integer orderNum) {
            this.orderNum = orderNum;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public Date getUpdateTime() {
            return updateTime;
        }
    
        public void setUpdateTime(Date updateTime) {
            this.updateTime = updateTime;
        }
    }
    

    CategoryServiceImpl方法:

    @Override
        @Cacheable(value = "listCategoryForCustomer")
        public List<CategoryVO> listCategoryForCustomer(Integer parentId) {
            ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
            recursivelyFindCategories(categoryVOList, parentId);
            return categoryVOList;
        }
    
        private void recursivelyFindCategories(List<CategoryVO> categoryVOList, Integer parentId) {
            //递归获取所有子类别,并组合成为一个“目录树”
            List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
            if (!CollectionUtils.isEmpty(categoryList)) {
                for (int i = 0; i < categoryList.size(); i++) {
                    Category category = categoryList.get(i);
                    CategoryVO categoryVO = new CategoryVO();
                    BeanUtils.copyProperties(category, categoryVO);
                    categoryVOList.add(categoryVO);
                    recursivelyFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
                }
            }
        }
    

    错误,

    空指针
    

    解决

    CategoryVO类中的childCategory属性,这是接口引用,没有进行初始化,改成

    List<CategoryVo> childCategory=new ArrayList<>();
    
  • 相关阅读:
    C#学习五
    C#学习二
    C#学习五
    完成车牌识别,自行拍车牌图片进行上传并查看结果
    C#学习
    简述ASP.NET网站开发步骤
    C#学习四
    C#学习三
    完成身份证识别,自行拍摄身份证图片进行上传并查看结果
    C#学习总结
  • 原文地址:https://www.cnblogs.com/little-mao2020/p/14812498.html
Copyright © 2011-2022 走看看