Browse Source

代码优化:排序空指针异常,查询空指针异常处理

danch 2 months ago
parent
commit
4c32f9b567

+ 2 - 1
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/controller/CategoryController.java

@@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -63,7 +64,7 @@ public class CategoryController extends JeecgController<Category, ICategoryServi
         QueryWrapper<Category> queryWrapper = QueryGenerator.initQueryWrapper(category, req.getParameterMap());
         List<Category> list1 = categoryService.queryTreeListNoPage(queryWrapper);
         IPage<Category> pageList = new Page<>(1, 10, list1.size());
-        List<Category> list = list1.stream().sorted(Comparator.comparingInt(p -> p.getSortNo())).collect(Collectors.toList());
+        List<Category> list = list1.stream().sorted(Comparator.comparingInt(p -> Optional.ofNullable(p.getSortNo()).orElse(0))).collect(Collectors.toList());
         pageList.setRecords(list);
         System.out.println("hasQuery:" + hasQuery);
         return Result.OK(pageList);

+ 4 - 7
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/service/impl/CategoryServiceImpl.java

@@ -14,10 +14,7 @@ import org.jeecg.modules.kms.knowledge.entity.Article;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Comparator;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -107,7 +104,7 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
     @Override
     public List<Category> queryTreeListNoPage(QueryWrapper<Category> queryWrapper) {
         List<Category> dataList1 = baseMapper.selectList(queryWrapper);
-        List<Category> dataList = dataList1.stream().sorted(Comparator.comparingInt(p -> p.getSortNo())).collect(Collectors.toList());
+        List<Category> dataList = dataList1.stream().sorted(Comparator.comparingInt(p -> Optional.ofNullable(p.getSortNo()).orElse(0))).collect(Collectors.toList());
         List<Category> mapList = new ArrayList<>();
         for (Category data : dataList) {
             String pidVal = data.getPid();
@@ -148,7 +145,7 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
         List<SelectTreeModel> selectTreeModels = baseMapper.queryListByPidPrivilege(pid, null, sql);
         //获取公共数据
         selectTreeModels.addAll(baseMapper.queryListByPidNoPrivilege(pid, null));
-        selectTreeModels.stream().sorted(Comparator.comparingInt(p -> p.getSort())).collect(Collectors.toList());
+        selectTreeModels.stream().sorted(Comparator.comparingInt(p -> Optional.ofNullable(p.getSort()).orElse(0))).collect(Collectors.toList());
         return selectTreeModels;
     }
 
@@ -164,7 +161,7 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
         List<SelectTreeModel> selectTreeModels = baseMapper.queryListByPidPrivilege(pid, null, sql);
         //获取公共数据
         selectTreeModels.addAll(baseMapper.queryListByPidNoPrivilege(pid, null));
-        selectTreeModels.stream().sorted(Comparator.comparingInt(p -> p.getSort())).collect(Collectors.toList());
+        selectTreeModels.stream().sorted(Comparator.comparingInt(p -> Optional.ofNullable(p.getSort()).orElse(0))).collect(Collectors.toList());
         return selectTreeModels;
     }
 

+ 7 - 2
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/knowledge/controller/ArticleController.java

@@ -75,8 +75,13 @@ public class ArticleController extends JeecgController<Article, IArticleService>
         IPage<Article> pageList = articleService.page(page, queryWrapper);
         // 获取CategoryId,如果CategoryId删除就获取他的父id以此类推
         for (Article articleFor : pageList.getRecords()) {
-            List<Category> categories = categoryService.queryParentNodeById(articleFor.getCategoryId());
-            articleFor.setCategoryId(categories.get(0).getId());
+            if (articleFor.getCategoryId() != null) {
+
+                List<Category> categories = categoryService.queryParentNodeById(articleFor.getCategoryId());
+                if (categories.size() > 0) {
+                    articleFor.setCategoryId(categories.get(0).getId());
+                }
+            }
         }
         return Result.OK(pageList);
     }

+ 3 - 2
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/knowledge/service/impl/ArticleServiceImpl.java

@@ -25,6 +25,7 @@ import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -61,8 +62,8 @@ public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> impl
                 article.setTags(tags);
             }
         }
-        List<Article> collect = articles.stream().sorted(Comparator.comparing(Article::getTitle)).collect(Collectors.toList());
-        articleIPage.setRecords(collect);
+        List<Article> collect = articles.stream().sorted(Comparator.comparing(article -> Optional.ofNullable(article.getTitle()).orElse("")))
+                .collect(Collectors.toList());
         return articleIPage;
     }