title: Spring mvc selecting list items using check box
body: <p>I would like to select some or all of the list elements out of populated list and fetch those at the controller which is giving NulllPointerException when fetching at .POST controller, details are..</p>
  
  <pre>Model Class</pre>
  
  <pre><code>public class Invoice {
              private int number;
              private String name;
              private String description;
              private Date date;
              private Double amount;
              private Double due;
  
                     .........setters and  getters ....
                 }
  </code></pre>
  
  <p>Form Backing Object:</p>
  
  <pre><code>public class Invoices {
  
                      List&lt;Invoice&gt; invoiceList;
                      ......... setter and getter  .....
                    }
  </code></pre>
  
  <p>Controller to show form is </p>
  
  <pre><code>Controller is:                             
      @RequestMapping(value = "/viewForm", method = RequestMethod.GET)
  public String viewInvoice(Model model) {
      List&lt;Invoice&gt;invoiceList = new CreateInvoiceItem().createItem();
      Invoices invoices = new Invoices();
      invoices.setInvoiceList(invoiceList);
      model.addAttribute("invoices",invoices);
      return "invoicelist";
  }
  </code></pre>
  
  <p>I have used a class to generate some data dynamically using</p>
  
  <pre><code> CreateInvoiceItem().createItem();
  </code></pre>
  
  <p>to show the form in jsp the code is: </p>
  
  <pre><code>    &lt;form:form action="viewSelectedList" path="invoices" 
        method="POST" commandName="invoices"&gt; 
       &lt;table&gt; &lt;tr&gt; &lt;td&gt;Action&lt;/td&gt; &lt;td&gt;#&lt;/td&gt; &lt;td&gt;Name&lt;/td&gt;
                &lt;td&gt;Description&lt;/td&gt; &lt;td&gt;Date&lt;/td&gt;
                    &lt;td&gt;Amount&lt;/td&gt;  &lt;td&gt;Due&lt;/td&gt; &lt;/tr&gt;
   &lt;jstl:forEach var="list" items="${invoices.invoiceList}" &gt;
    &lt;tr&gt;  &lt;td&gt;&lt;input type="checkbox" value="${list.number}"  
                   id="${list.number}" name="${list.number}"/&gt;&lt;/td&gt;
      &lt;td&gt;&lt;jstl:out value="${list.number}"/&gt; &lt;/td&gt;
      &lt;td&gt;&lt;jstl:out value="${list.name}"/&gt; &lt;/td&gt;
      &lt;td&gt;&lt;jstl:out value="${list.description}"/&gt; &lt;/td&gt;
      &lt;td&gt;&lt;fmt:formatDate pattern="dd-MM-yyyy"    
                       value="${list.date}" /&gt;&lt;/td&gt; 
      &lt;td&gt;&lt;jstl:out value="${list.amount}"/&gt; &lt;/td&gt;
      &lt;td&gt;&lt;jstl:out value="${list.due}"/&gt; &lt;/td&gt;
   &lt;/tr&gt;
  &lt;/jstl:forEach&gt; 
  &lt;tr&gt;&lt;td&gt;&lt;input type="submit" name="submit" value="Submit"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
      &lt;/form:form&gt; 
  </code></pre>
  
  <p>Upto here it is working fine the output is :
  <a href="https://i.stack.imgur.com/lPP9Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lPP9Z.png" alt="enter image description here"></a></p>
  
  <p>But when clicking on the submit button the .POST controller is showing the NullPopinterException:</p>
  
  <pre><code>@RequestMapping(value = "/viewSelectedList", method = RequestMethod.POST)
  public String showList(Model model, Invoices invoices,
  BindingResult result) {
  List&lt;Invoice&gt; selectedList = invoices.invoiceList;
  model.addAttribute(invoices);
  return "selectedList";
  }
  </code></pre>
  
  <p>The selected list is not available in POST controller. I have seen many examples but I could not understand where I am going wrong.</p>