[英]Null Pointer Exception while trying to print Parent - Child Parent relationship using Eclipselink JPA + Spring 3
I have the following bidirectional relationship : A Course can have several assignments. 我具有以下双向关系:一门课程可以有多个作业。
I am getting a Null Pointer exception when I try the following: 当我尝试以下操作时,出现Null Pointer异常:
I have the following methods in CourseController.java 我在CourseController.java中具有以下方法
@RequestMapping(value="view/{id}/assignment/create")
public ModelAndView createAssignment(@PathVariable("id") String id) throws Exception {
ModelAndView modelAndView = new ModelAndView("course/assignment/create");
Assignment assignment = new Assignment();
assignment.setCourse(this.courseManager.getCourse(id));
//System.out.println(assignment.getCourse().getName()); This prints the course name correctly
modelAndView.addObject("assignment", assignment);
return modelAndView;
}
This is the jsp:
<form:form action="/cbass/assignment/check" method="POST" commandName="assignment">
<form:errors path="*" cssClass="errorblock" element="div"/>
Course Name: ${assignment.course.name}
<table>
<tr>
<td>Name : </td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
ANd this is where the request goes from the JSP. 这是来自JSP的请求。
@RequestMapping(value = "check", method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("assignment") Assignment assignment,
BindingResult result, SessionStatus status) {
assignmentValidator.validate(assignment, result);
System.out.println(assignment.getCourse().getName());
if (result.hasErrors()) {
//if validator failed
return "course/assignment/create";
} else {
status.setComplete();
//form success
System.out.println("Hello"+assignment.getCourse().getName());
this.assignmentManager.create(assignment);
return "course/assignment/view";//+assignment.getId();
}
}
Although I am able to see the course name properly in the JSP page, 尽管我可以在JSP页面上正确看到课程名称,
Course Name: ${assignment.course.name}
I still get a Null Pointer Exception when I try to execute this: 尝试执行此操作时,我仍然收到Null Pointer异常:
assignment.getCourse().getName()
I am new to Spring and EclipseLink JPA. 我是Spring和EclipseLink JPA的新手。 Please help. 请帮忙。
Seems like your course is null. 好像您的课程为空。 Ensure you are maintaining both sides of the relationship. 确保您维护关系的双方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.