【问题标题】:Null pointer Exception in Spring Boot Data JpaSpring Boot Data Jpa 中的空指针异常
【发布时间】:2020-10-14 10:12:20
【问题描述】:

控制器

@RequestMapping(value=ApiName.BLOG_LIST_FEW, method=RequestMethod.POST,produces=ApiName.CHARSET)
        public ResponseEntity<ApiResponse> blogListFew(@RequestBody String user){
            try {
                UserReqModel userReq=JsonUtill.convertJsonToJava(user,UserReqModel.class);
                Map<String,Object> resp=userService.blogListFew(userReq.getPageNo());
                if( !resp.containsKey("error")) {
                    return responseUtil.successResponse(resp);
                }
                else {
                    return responseUtil.failRsponse(resp.get("error"));
                }
                
            }
            catch(Exception ex) {
                
                return responseUtil.badResponse(ex);
            }
        }

服务

public Map<String, Object> blogListFew(Integer pageNo) {
        Map<String, Object> resp = new HashMap<>();
        List<Map<String, String>> arlMap = new ArrayList<>();
        try {

            int pageSize = 4;

            System.out.println("here in the service 1");

            System.out.println("here in the service 2");
            Page<Blog> blogList = blogRepo.findByIsActiveOrderByCreateDateDesc(YesNO.YES,
                    PageRequest.of(pageNo, pageSize));
            System.out.println("blogListb-->"+blogList);
            
            if (blogList == null) {
                resp.put("error", "not found");
                return resp;
            }
                blogList.forEach(blog -> {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("description",
                            (blog.getDescription() != null) ? blog.getDescription().substring(0, 1) + "..." : "");
                    hashMap.put("blogTitle", (blog.getBlogTitle() != null) ? blog.getDescription() : "");
                    hashMap.put("verticalImageUrl", (blog.getImageVertUrl() != null) ? blog.getImageVertUrl() : "");
                    hashMap.put("blogId", blog.getBlogId().toString());
                    hashMap.put("isTrendingPriority", Integer.toString(blog.getTrendingPriority()));
                    arlMap.add(hashMap);
                });
                resp.put("totalPage", blogList.getTotalPages());
                resp.put("blogList", arlMap);

            
    
            
            return resp;

        } catch (Exception ex) {
            System.out.println(ex);
            System.out.println(ex.getMessage());
            resp.put("error","sxception occured");
            
            return resp;
        }

    }

控制台输出-->

here in the service 1
here in the service 2
java.lang.NullPointerException
null

我正在检查没有博客表记录的情况,那次我调用了我的 api,但那时我知道它应该返回 null,因为我已经检查过,我会返回一些响应,但我不知道我是怎么做的我正在收到空指针异常,因为当它为空时甚至无法执行任何操作,甚至尝试打印 blogList 但即使该语句未执行,这意味着它在 jpa 查询方法中给出错误,并且当其中有记录时它工作正常.

我不知道它是如何获得空指针异常的,正是因为这个原因,我才在 bloglist 上进行了检查。 请任何人帮助我解决这个问题

【问题讨论】:

    标签: spring-boot spring-data-jpa spring-data


    【解决方案1】:

    你需要检查几件事

    • 您需要检查 BlogRepo 是否为@Autowired

    • 您还可以在 System.out 中打印 BlogRepo,以便确定它是否为空

    • 你需要检查你是否有@EnableJpaRepositories

      @EnableJpaRepositories(basePackages = "com.persistence.dao")

    【讨论】:

    • 它是自动装配的,我已经在打印它时给出了类名,如 org.springframework.data.jpa.repository.support.SimpleJpaRepository@147990de 并且它在基本包中你能告诉更多原因吗为什么我无法理解当有记录时它正在工作
    • 你可以使用 ex.printStackTrace(); 代替 System.out.println(ex); 以便我们可以精确定位NPE的,也请pr pageNo
    【解决方案2】:

    您是否将 blogRepo 注入到控制器中?

    @Autowire

    {BlogRepo_class_name} blogRepo

    其中“BlogRepo_class_name”是您的 blogRepo 类的名称。

    【讨论】:

    • 它已经添加到我的代码中 @Autowired private BlogRepo blogRepo;
    • 是的,我注入了控制器,证明当其中存在记录时它可以工作
    猜你喜欢
    • 2018-12-31
    • 2019-02-03
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2021-06-15
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多