Hibernate Spring Object Wrapper not passing values
I am struggling to create a form where I can insert multiple readings of electric meters. I have constructed a wrapper that holds all the values. All looks good until the moment I submit the readings as I get java.lang.NullPointerException.
Here is part of a controller:
@RequestMapping("/registerEnergyReadings")
public ModelAndView registerEnergyReadings(
@ModelAttribute("energyReadingWrapper") EnergyReadingWrapper, energyReadingWrapper, BindingResult result) {
// loading available meters from DB:
List<EnergyMeters> energyMeters= meterService.getMeters();
List<EnergyReading> readings= new ArrayList<EnergyReading>();
//preparing the wrapper
for (int i = 0; i < energyMeters.size(); i++) {
EnergyReading reading = new EnergyReading();
reading.setMeter(energyMeters.get(i));
//the two lines below done temporary so I don't have to fill the form every time
// I test it now
reading.setReading(23);
reading.setDate(new Date());
readings.add(reading);
}
energyReadingWrapper.setEnergyReadings(readings);
Map<String, Object> model = new HashMap<String, Object>();
model.put("energyReadingWrapper", energyReadingWrapper);
return new ModelAndView("RegisterEnergyReadings", "model", model);
}
@RequestMapping("/saveEnergyReadings")
public ModelAndView saveEnergyReadings( @ModelAttribute("energyReadingWrapper") EnergyReadingWrapper energyReadingWrapper, BindingResult result) {
System.out.println("this is the point I reach before the null exception");
System.out.println(energyReadingWrapper.toString());
List<EnergyReadings> readings= energyReadingWrapper.getEnergyReadings();
for (EnergyReadingsi : readings) {
readingService.saveReading(i);
}
return new ModelAndView("redirect:/readingList.html");
}
And the jsp form (the crucial part of it):
<c:url var="readings" value="saveEnergyReadings.html" />
<form:form id="energyReadingWrapper"
modelAttribute="energyReadingWrapper" method="post"
action="${readings}">
<table width="400px" height="150px">
<c:forEach items="${energyReadingWrapper.energyReadings}"
varStatus="i" var="energyReadings">
<tr>
<td><c:out value="${energyReadings.meter.description}" /></td>
<td><form:input
path="energyReadings[${i.index}].readingValue" /></td>
<td><form:input path="energyReadings[${i.index}].date"
type="date" /></td>
</tr>
</c:forEach>
<tr>
<td colspan="6" align="center"><input type="submit"
value="Save" /></td>
</table>
</form:form>
All looks fine. The jsp page generates table with rows matching the number of meters and all of them have the default values that I set (23 and the current date) I don't understand why the wrapper is not being passed to the controller when I press save button. What have I missed?
Regards, Maciej
EDIT: The stack trace (unfortunately I am writing the program in Polish):
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause
java.lang.NullPointerException forms.OdczytEnergiaFormularz.toString(OdczytEnergiaFormularz.java:20) kamienica.controller.HomeController.zapiszOdczytEnergia(HomeController.java:511) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710) org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414) org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:77 1) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Here is the wrapper class:
public class EnergyReadingWrapper{
private List<EnergyReading> energyReadings;
(setters and getters)
}
链接地址: http://www.djcxy.com/p/27888.html
上一篇: 无法将命令发送到SMTP主机