title: SpringMVC model attribute returns null on post
body: <p>I was developing a SpringMVC 3 trivial application, but I got stuck somewhere. Essentially, a model attribute whose fields are filled in the GET operation are returned NULL in the POST (even if I don't make any operation on them). I've checked on this and other forums and the only answer I came up with was to implement Editors for the classes I should put into the model, an initializer that could be used to register custom editors and make it available to the application (in the servletname-servlet.xml file). All operations that I did, but definitely no luck. I was wondering if someone out there could give me a hand. 
  Thank you.  </p>
  
  <p>The following controller:</p>
  
  <pre><code>@Controller
  @RequestMapping(value="/nourish")
  public class NourishController {
  
  private PetDAO pdao = new PetDAO();
  private UserDAO udao = new UserDAO();
  private FeedVirtualPet feedvp = new FeedVirtualPet();
  
  @RequestMapping(method = RequestMethod.GET)
  public String nourish(Model model, HttpServletRequest request){
      NourishPetDTO npdto = new NourishPetDTO();
      PetDTO      pdto=pdao.findPetByBusinessKey((PetDTO)request.getSession().getAttribute("pet"));
      npdto.setPet(pdto);
      npdto.setAmt(0);
      model.addAttribute("npdto", npdto);
      return "nourish";
  }
  
  
  @RequestMapping(method = RequestMethod.POST)
  public String nourishPOST(@ModelAttribute("npdto") NourishPetDTO npdto,
          //BindingResult result,
          HttpServletRequest request){
  
      System.out.println("*****nourishPOST.npdto.amt: "+npdto.getAmt());
      System.out.println("*****nourishPOST.npdto.pet.petname: "+npdto.getPet().getPetName());
      System.out.println("*****nourishPOST.npdto.pet.hunger:     "+npdto.getPet().getHunger());
      PetDTO pdto = feedvp.feed(npdto.getPet(), npdto.getAmt());
      request.getSession().setAttribute("user", pdto.getOwner());
      return "redirect:detailPet";
  }
  }
  </code></pre>
  
  <p>has methods for both GET and POST operations, and is associated to the following jsp - in this view, all the model informations are correctly displayed through EL:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" session="true"  pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Nourish your pet!!&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  Your stats for &lt;h3&gt;${npdto.pet.petName}&lt;/h3&gt;&lt;br&gt;
      Please note that any value you put inside will:
          &lt;ol&gt;
              &lt;li&gt;Subtract value to your current hunger level&lt;/li&gt;
              &lt;li&gt;Add (value) to your current health level&lt;/li&gt;
          &lt;/ol&gt;
      Please note that any value you'll put will in many manners "resized":
      &lt;ol&gt;
          &lt;li&gt;It must be even. If not, a default 0 value will be  applied&lt;/li&gt;
          &lt;li&gt;It can't be greater than 4. If it's greater, the maxium value  of 4 will be anyway considered.&lt;/li&gt;
          &lt;li&gt;If it ain't a number, a default zero value will be passed&lt;/li&gt;
      &lt;/ol&gt;
  &lt;table&gt;
      &lt;tr&gt;&lt;td&gt;Health&lt;/td&gt;&lt;td&gt;${npdto.pet.health}&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;Hunger&lt;/td&gt;&lt;td&gt;${npdto.pet.hunger}&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;form action="nourish" method="post"  &gt;
      nourishment: &lt;input type="text" name="amt"/&gt;
      &lt;input type="submit" value="Nourish!"/&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>(Please note how I didn't use the model attribute that's returning NULL, to be sure I wasn't doing anything to it)</p>
  
  <p>The POST operations fail on the instruction </p>
  
  <pre><code>System.out.println("*****nourishPOST.npdto.pet.petname:"+npdto.getPet().getPetName());
  </code></pre>
  
  <p>as Tomcat returns a NullPointerException.</p>
  
  <p>As aforementioned, I have been searching a solution to this problem, and everything I could find is to add Editor classes &amp; register Editors to a binder.  Result is still the same. </p>
  
  <p>Anyway, these are the classes:</p>
  
  <p>NourishPetEditor.java</p>
  
  <pre><code>public class NourishPetEditor extends PropertyEditorSupport {
  
  private PetEditor pedit;
  
  public PetEditor getPedit() {
      return pedit;
  }
  
  
  public NourishPetEditor() {
      // TODO Auto-generated constructor stub
      pedit =  new PetEditor();
  }
  
  @Override 
  public String getAsText(){
      NourishPetDTO npdto= (NourishPetDTO)getValue();
      return super.getAsText()+","+npdto.getAmt();
  }
  
  public NourishPetDTO makeNourishPetDTOInstance(String [] parts){
  
      NourishPetDTO npdto = new NourishPetDTO(); 
      npdto.setPet(pedit.makePetDTOInstance(parts));
      npdto.setAmt(Integer.parseInt(parts[9]));
      return npdto;
  
  }
  
  
  public void setAsText(String key){
      String []parts = key.split(",");
      NourishPetDTO npdto = makeNourishPetDTOInstance(parts);
      setValue(npdto);
  }
  }
  </code></pre>
  
  <p>PetEditor.java</p>
  
  <pre><code>package com.virtualpet.dtoeditors;
  import java.beans.PropertyEditorSupport;
  
  import com.virtualpet.virtualpet_daos.PetDAO;  
  import com.virtualpet.virtualpet_dtos.PetDTO;
  import com.virtualpet.virtualpet_dtos.UserDTO;
  public class PetEditor extends PropertyEditorSupport{
  
  private PetDAO pdao;
  
  public PetEditor() {
      // TODO Auto-generated constructor stub
  }
  
  public PetEditor(PetDAO pdao) {
      // TODO Auto-generated constructor stub
      this.pdao = pdao;
  
  }
  
  
  public String getAsText(){
      PetDTO pdto = (PetDTO) this.getValue();
  
      return pdto.getClass().getName()+","+ //0
      pdto.getPetName()+","+ //1
      pdto.getHealth()+","+  //2
      pdto.getHunger()+","+  //3
      pdto.getMood()+","+","+ //4
      pdto.getOwner().getClass().getName()+","+ //5
      pdto.getOwner().getUsername()+","+ //6
      pdto.getOwner().getEmail()+","+pdto.getOwner().getPassword(); //7,8
  
  }
  
  public void setAsText(String key) throws IllegalArgumentException {
      String []parts = key.split(",");
      PetDTO pdto = makePetDTOInstance(parts);
      setValue(pdto);
  }
  
  
  public UserDTO makeUserDTOInstance(String[] parts)
          throws InstantiationException, IllegalAccessException,
          ClassNotFoundException {
  
              UserDTO udto = (UserDTO)Class.forName(parts[5]).newInstance();
              udto.setUsername(parts[6]);
              udto.setEmail(parts[7]);
              udto.setPassword(parts[8]);
              return udto;
  }
  
  
  public PetDTO makePetDTOInstance(String[]parts){
      try{
          PetDTO pdto = (PetDTO) Class.forName(parts[0]).newInstance();
          pdto.setPetName(parts[1]);
          pdto.setHealth(Integer.parseInt(parts[2]));
          pdto.setHunger(Integer.parseInt(parts[3]));
          pdto.setMood(Integer.parseInt(parts[4]));
  
          UserDTO udto = makeUserDTOInstance(parts);
  
          pdto.setOwner(udto);
          return pdto;
      }
      catch (Exception e){
          throw new IllegalArgumentException();
      }
  }
  }
  </code></pre>
  
  <p>I'll spare you UserEditor, as it's pretty much similar to PetEditor.
  Finally, the Initializer to bind the custom editors &amp; the Model Classes.</p>
  
  <p>VirtualPetDTOInitializer.java</p>
  
  <pre><code>package com.virtualpet.dtoeditors;
  
  import org.springframework.web.bind.WebDataBinder;
  import org.springframework.web.bind.support.WebBindingInitializer;
  import org.springframework.web.context.request.WebRequest;
  
  import com.virtualpet.virtualpet_dtos.NourishPetDTO;
  import com.virtualpet.virtualpet_dtos.PetDTO;
  import com.virtualpet.virtualpet_dtos.UserDTO;
  
  public class VirtualPetDTOInitializer implements WebBindingInitializer {
  
  public void initBinder(WebDataBinder binder, WebRequest arg1) {
      // TODO Auto-generated method stub
          binder.registerCustomEditor(UserDTO.class, new UserEditor( ));
          binder.registerCustomEditor(PetDTO.class, new PetEditor( ));
          binder.registerCustomEditor(NourishPetDTO.class, new NourishPetEditor());
  }
  }
  </code></pre>
  
  <p>This class defines a property value in dispatcher-servlet.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans ...&gt;
  &lt;bean id="userDao"
      class="com.virtualpet.virtualpet_daos.UserDAO"/&gt;
  &lt;bean id="petDao"
      class="com.virtualpet.virtualpet_daos.PetDAO" /&gt;
  &lt;bean    class="org.springframwork.web.servlet.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
      &lt;property name="webBindingInitializer"&gt;
          &lt;bean class="com.virtualpet.dtoeditors.VirtualPetDTOInitializer"/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Being a total rookie on Spring MVC, I must tell you that this error is something I got even before I implemented these classes. So, looks like they're not a factor and yet, their implementation is everything I could find aboud model attribute returned null after POST.
  Could anyone please help? Thanks in advance. </p>