spring bootjpa mysql nullpointer save/update异常

kfgdxczn  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(272)

我正在尝试做保存或更新,根据我的 Spring 启动myselaf,jpa,mysql应用程序的要求。但我得到nullpointer异常更新。
这是我的控制器的要求

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

    }

下面是我的服务类的实现

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

对于相同的视图,如下所示
insert和update页面上的数据绑定如下所示

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

然后返回错误500页

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

错误的堆栈跟踪如下

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]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题