0

Controller

@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);
            }
        }

Service

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;
        }

    }

output in console-->

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

I was checking for the condition when there is no record for blog table and that time I called my api but that time i know it should return null for that I have check and i will return some respones but I dont know how I am getting null pointer exception in that because no way any operation performed when it will be null even tried to print blogList but even that statement not executed that means it is giving error in jpa query method and it is working fine when there are records in it.

I dont know how it is getting null pointer Exception for that very reason i put the check on bloglist. Please anyone help me with this

2 Answers 2

1

you need to check couple of things

  • You need to check if BlogRepo is @Autowired

  • you can also print BlogRepo in your System.out so that you get sure if it is null or not

  • You need to check if you have @EnableJpaRepositories

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

Sign up to request clarification or add additional context in comments.

2 Comments

it is autowired and I already and on printing it is giving class name like org.springframework.data.jpa.repository.support.SimpleJpaRepository@147990de and it is in base package can you tell more the reason why I am not able to understand is it is working when there are records present
can you use ex.printStackTrace(); instead of System.out.println(ex); so that we can pin point exact line of NPE, also please pring pageNo
1

Did you inject your blogRepo into the controller?

@Autowire

{BlogRepo_class_name} blogRepo

where "BlogRepo_class_name" is the name of your blogRepo class.

2 Comments

its already added in my code @Autowired private BlogRepo blogRepo;
yes i injected into the controller and the proof is it works when there are records present in it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.