title: Spring and forms with select
body: <p>I am using Spring to populate a  item in a form, using object passing between controller and views:</p>
  
  <h1>Domain Objects</h1>
  
  <pre><code>public class Client {
  private String name;
  private int id;
  private Bar bar;
  // getters and setters omitted
  }
  
  
  public class Bar {
  private Integer id;
  private String name;
  // getters and setters omitted
  
  }
  </code></pre>
  
  <h1>Controller</h1>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET, value = "/")
  public String index(Model m) {
  m.addAttribute("Client", new Client());
  Bar o1 = new Bar();
  o1.setName("n 1");
  o1.setId(1);
  Bar o2 = new Bar();
  o2.setName("n 2");
  o2.setId(2);
  Bar o3 = new Bar();
  o3.setName("n 3");
  o3.setId(3);
  Bar o4 = new Bar();
  o4.setName("n 4");
  o4.setId(4);
  Bar o5 = new Bar();
  o5.setName("n 5");
  o5.setId(5);
  List&lt;Bar&gt; orders = new ArrayList&lt;Bar&gt;();
  orders.add(o1);
  orders.add(o2);
  orders.add(o3);
  orders.add(o4);
  orders.add(o5);
  m.addAttribute("ordersList", orders);
  return "index";
  }
  
  @RequestMapping(method = RequestMethod.POST, value = "/")
  public String indexPost(@ModelAttribute("Client") Client g, BindingResult r) {
  log.info(g.getId() + " " + g.getName());
  log.info(g.getBar().getName());
  return "index";
  }
  </code></pre>
  
  <h1>View (index.jsp)</h1>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;body&gt;
  &lt;form:form method="POST" modelAttribute="Client"&gt;
  &lt;form:label path="name"&gt;Name&lt;/form:label&gt;
  &lt;form:input path="name" /&gt;
  &lt;form:label path="id"&gt;Id&lt;/form:label&gt;
  &lt;form:input path="id" /&gt;
  &lt;form:label path="orders"&gt;Order&lt;/form:label&gt;
  &lt;form:select path="bar" items="${ordersList}" /&gt;    
  &lt;form:button&gt;Insert&lt;/form:button&gt;
  &lt;/form:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>When I fill out the form, and make the submit, the controller throw a NullPointerException on <code>log.info(g.getBar().getName());</code> 
  I assume it is because getBar() returns <code>null</code>.
  Where am I wrong with this data binding?</p>