我正在尝试按照我的spring boot thymeleaf,jpa,mysql应用程序中的要求进行保存或更新。但我得到更新的NullPointer异常。
这是我的Controller的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();
}
和我的服务类的实现在
之下@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);
}
百里香的观点与以下相同
关于插入和更新页面的数据绑定如下所示
<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]