1

I am trying to do Save or Update as per the requirement in my spring boot thymeleaf, jpa, mysql application. But I am getting NullPointer Exception for Update.

here is my Controller's ReqestMethods

 @GetMapping("/otherMarks/{id}")
    public ModelAndView addOtherMarks(@PathVariable("id") Long id, Model model){

        ModelAndView modelAndView = new ModelAndView();

        OtherMarksDto odto=new OtherMarksDto();
        Students students=studentService.getStudentById(id);
        odto.setId(id);
        odto.setName(students.getName());
        odto.setRollNumber(students.getRollNumber());
        odto.setClassSection(students.getClassSection());


  //  PRESENTING UPDATE IF DATA IS ALREADY THERE

        if(students.getOtherMarksSet().isEmpty()!=true){

            odto.setOtherMarksSet(students.getOtherMarksSet());
            for(OtherMarks m:students.getOtherMarksSet()){
                odto.setMid(m.getId());
            }


            modelAndView.setViewName("editOtherMarks");
        }

    // PRESENTING INSERT IF THERE IS NO DATA

        else
            modelAndView.setViewName("otherMarks");

        model.addAttribute("marks", odto);
                    return modelAndView;
    }


    @PostMapping("/otherMarks/{id}")
    public String postInsertOtherMarks ( @ModelAttribute("marks") OtherMarksDto marks){


        otherMarksService.save(marks);
        System.out.println("CALL FROM PostInsertOtherMarks ");
        System.out.println("mid is : "+marks.getMid());
        Students students=studentService.getStudentById(marks.getId());



        return  "redirect:/otherMarks/"+students.getClassSection();


    }

and My service class's implementation is below

@Override
public void save( OtherMarksDto marks) {
    Students students=studentRepo.findById(marks.getId());

    OtherMarks otherMarks;
    if(students.getOtherMarksSet().isEmpty()!=true){
        otherMarks=otherMarksRepository.findOne(marks.getMid());

    }
    else
        otherMarks=new OtherMarks();


    /* 
       *  REST OF THE CODE GOES HERE
   */


// AND THEN I AM SAVING THE MARKS
students.addOtherMarks(otherMarks);

    otherMarksRepository.save(otherMarks);
}

thymeleaf view for the same is as following

DATA BINDING ON BOTH INSERT AND UPDATE PAGES ARE LIKE BELOW

<form  th:action="@{/otherMarks/{id}}" th:object="(${marks})" method="POST">
// THEN GOES DATA BINDING FOR FIELDS 

and then its returning error 500 page which says

status":500,"error":"Internal Server 
Error","exception":"java.lang.NullPointerException","message":"No message 
available","path":"/otherMarks/%7Bid%7D"}

stack trace of error is below

 ERROR 11528 --- [nio-8070-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    
: Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is 
java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
at 
com.pdfjar.service.OtherMarksServiceImpl.save(OtherMarksServiceImpl.java:63) 
~[classes/:na]
atat 
com.pdfjar.controller.MarksController.postInsertOtherMarks
(MarksController.java:175) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~ 
[na:1.8.0_101]
at 

sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:62)
 ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101] 
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Gaurav
  • 13
  • 6
  • Which is line 63 in OtherMarksServiceImpl? – holmis83 Apr 12 '18 at 09:36
  • You mean this students.addOtherMarks(otherMarks); I am adding otherMarks object to a set in class Students – Gaurav Apr 12 '18 at 09:42
  • Thanks holmis83 for pointing it out, I checked there was a silly typing mistake in my editOtherMarks.html. I corrected it. It's working now – Gaurav Apr 12 '18 at 12:09
  • Possible duplicate of [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) – holmis83 Apr 12 '18 at 12:11
  • Not exactly, actually I had fields for physical education marks with name pEducation and pEducation2, and I mistakenly put both as pEducation like this for the second one name="pEducation" value="pEducation" th:attr="${pEducation2}" may be result of copy paste from.upper line, I missed to change the fields name and value – Gaurav Apr 12 '18 at 14:30

0 Answers0