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