0

I want to fetch third level children I try this ways but it is not working.

{
   "title": "box", 
   "active": false,
   "level": "you",
   "children": [
        {
            "id": 1,
            "title": "hood",
            "type": "uod",
            "active": false,
            "level": "9",
            "children": [
             { 
                 "id":1,
                "title": "New",
                "type": "hog",
                "active": false,
                "level": "3",
                 }
             ]
        }
   ]
}

I want to find one nested record with Id in mongo db spring boot. But getting NullPointerException how to handle it with this code.

This my Get Apis



    @GetMapping("/menus/{id}/submenu/{subId}/children/{childId}")
    public List<?> findChildrenById(@PathVariable("id") long id, 
    
        @PathVariable("subId") long subId, @PathVariable("childId") long childId) { 
            Query query = new Query(); 
            query.addCriteria(Criteria.where("id").is(id).and("children.id").is(subId).and("children.children.id").is(childId)); 
            return operations.findOne(query, SubMenu.class).getChildren();
        

    }

Error

"java.lang.NullPointerException: Cannot invoke "com.menu.entity.SubMenu.getChildren()" because the return value of "org.springframework.data.mongodb.core.MongoOperations.findOne(org.springframework.data.mongodb.core.query.Query, java.lang.Class)" is null\r\n\tat com..menu.controller.MenuItemsGetControlller.findChildrenById(MenuItemsGetControlller.java:84)\r\n\tat
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Jan 10 '23 at 11:00
  • you have two same path variables @PathVariable("id") long id, @PathVariable("Id") long subId – Vladimir Krylov Jan 10 '23 at 11:23
  • My question is when I m try to find children this is giving me null pointer . I know How to handle but in this case I m not able to think where and which condition I apply . see my error – tauheed Alzohar Jan 10 '23 at 11:34
  • findOne is supposed to return null when there is no matching document found, that is an expected return value that should be handled. – Joe Jan 11 '23 at 04:17

0 Answers0