0

I got NPE when trying to call getter. It is a @OneToMany mapped property. It works when I call via other than the current object.

It is not working for the below code.

@PostMapping("/updateservice")
    public void updateService(@ModelAttribute ServiceTyping serviceTyping, BindingResult bindingResult, HttpServletRequest req, HttpServletResponse res) throws IOException {

if(serviceTyping.getServiceEntryAttributes().containsKey(ServiceConstants.outsourcer)) {
            LOG.info("**INFO OUTSOURCER FOUND*******");
        }
}

But working when I accessed from other object

@PostMapping("/updateservice")
    public void updateService(@ModelAttribute ServiceTyping serviceTyping, BindingResult bindingResult, HttpServletRequest req, HttpServletResponse res) throws IOException {

ServiceTyping st = entryService.findTypingEntryById((long) 194);


if(st.getServiceEntryAttributes().containsKey(ServiceConstants.outsourcer)) {
            LOG.info("**INFO OUTSOURCER FOUND*******");
        }
}

I tried with hot-coding below code, I mean directly pass the current object's id. It also doesn't work.

@PostMapping("/updateservice")
    public void updateService(@ModelAttribute ServiceTyping serviceTyping, BindingResult bindingResult, HttpServletRequest req, HttpServletResponse res) throws IOException {

//here 176 is the id of the entity
ServiceTyping st = entryService.findTypingEntryById((long) 176);

    //checking the object which has the 176 is found are not
    if(entryService.findEntryById((long) 176)!=null) {
            LOG.info("**INFO OBJECT FOUND*******");
           if(entryService.findEntryById((long) 176).getServiceEntryAttributes().containsKey(ServiceConstants.outsourcer)) {
         LOG.info("**INFO OUTSOURCER FOUND*******"); }

    }


}

enter image description here I'm not saying it is not working for the particular id 176. It doesn't working for any other objects. but it works in the case when I access through the object other than the same object found in public void updateService(@ModelAttribute ServiceTyping serviceTyping)

From the code and attached table, I have proven two things. 1) the object through which I try to access is not null; 2) the value which I trying to get is found and it is also not null.

Let me know if I'm confusing

Udhaya
  • 121
  • 1
  • 14
  • Becasue `st` is null here. – Eklavya Apr 17 '20 at 15:06
  • Actually, when calling through `st` it is working fine. It throws `null pointer exception` when calling through `serviceTyping` (the first snippet) – Udhaya Apr 17 '20 at 15:22
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it not helps me – Udhaya Apr 17 '20 at 16:34
  • It is not duplicate one @rkosegi how you can say it is duplicate by title aone? Have you read the code? – Udhaya Apr 18 '20 at 03:39

0 Answers0