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<>();
    
  • 相关阅读:
    spring mvc自定义配置 mvc:annotationdriven 东师理想
    spring配置xmemcached及使用 东师理想
    fastjson的简单序列化使用(转) 东师理想
    配置springmvc + simplespringmemcached 东师理想
    利用Spring AOP 更新memcached 缓存策略的实现(二) 东师理想
    springmvc 退出清除session,返回首页,获取当前项目路径,返回前台ajax输出 东师理想
    利用Spring AOP 更新memcached 缓存策略的实现(一) 东师理想
    tomcat7 配置 jstl 东师理想
    Linear Sieve Method for Prime Numbers
    List All Of The Subsets
  • 原文地址:https://www.cnblogs.com/little-mao2020/p/14812498.html
Copyright © 2011-2022 走看看