title: Spring MVC Thymeleaf binding List with check boxes
body: <p>I am trying to create a form using thymeleaf that contains a series of checkboxes.  The object source that I am passing through to the thymeleaf template contains a String and a List.</p>
  
  <pre><code>package com.controller;
  
  import java.util.List;
  
  public class Source {
  private String sourceName;
  private List&lt;String&gt; testList;
  
  public String getSourceName()
  {
      return sourceName;
  }
  
  public void setSourceName(String name)
  {
      this.sourceName = name;
  }
  
  public List&lt;String&gt; getTestList()
  {
      return testList;
  }
  
  public void setTestList(List&lt;String&gt; list)
  {
      this.testList = list;
  }
  
  }
  </code></pre>
  
  <p>I pass an object of type source into the template using this MVC controller.</p>
  
  <pre><code>package com.controller;
  
  
  import java.io.IOException;
  import java.util.List;
  import java.util.Locale;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.servlet.View;
  import org.thymeleaf.spring4.view.ThymeleafViewResolver;
  
  import com.web_application.AllEnvironmentsFromFile;
  import com.web_application.AllTestsAndPaths;
  import com.web_application.RunDao;
  import com.web_application.TestAndPath;
  
  
  @RestController
  public class ManualTestController {
  @Autowired
  private ThymeleafViewResolver resolver;
  
  @Autowired
  RunDao rDao;
  
  @Autowired
  AllEnvironmentsFromFile environments;
  
  
  @RequestMapping(value="/manualTest", method=RequestMethod.GET)
  public View greetingForm(Model model) throws Exception {
  
      AllTestsAndPaths a = new AllTestsAndPaths();
      List&lt;TestAndPath&gt; testList = a.testsAndPaths();     
      String[] environmentList = new String[environments.getEnvironments().size()];
      for(int i = 0; i &lt; environments.getEnvironments().size(); i++)
      {
          environmentList[i] = environments.getEnvironments().get(i).getName();
      }
  
      model.addAttribute("testList", testList);
      model.addAttribute("source", new Source());
      model.addAttribute("environmentList", environmentList);
      return resolver.resolveViewName("manualTest", Locale.US);    }
  
  @RequestMapping(value="/manualTest", method=RequestMethod.POST)
  public String greetingSubmit(@ModelAttribute Source source, Model model) {
      System.out.println(source.getSourceName());
      for(String hello : source.getTestList())
      {
          System.out.println(hello);
      }
      model.addAttribute("source", source);
      return "result";
  }
  
  }
  </code></pre>
  
  <p>The manualTest template looks like this</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;form action="#" th:action="@{/manualTest}" th:object="${source}" method="post"&gt;
      &lt;p&gt;Source: &lt;input type="text" th:field="*{sourceName}" /&gt;&lt;/p&gt;
  
  &lt;table border="1"&gt;
  &lt;tr&gt;
      &lt;td&gt;Test Name&lt;/td&gt;
      &lt;td th:each="environment : ${environmentList}"
      th:text="${environment}"&gt;Tests&lt;/td&gt;
  &lt;/tr&gt;
  &lt;th:block th:each="test : ${testList}"&gt;
  &lt;tr&gt;
      &lt;td th:text="${test.name}"&gt;A Test'&lt;/td&gt;
      &lt;th:block th:each="enviro : ${environmentList}"&gt;
          &lt;td&gt;&lt;input type="checkbox" path="${testList}" value="hello" /&gt;&lt;/td&gt;
      &lt;/th:block&gt;
  
  &lt;/tr&gt;
  &lt;/th:block&gt;
  
  &lt;/table&gt;
  &lt;p&gt;&lt;input type="submit" value="Submit" /&gt; &lt;input type="reset" value="Reset" /&gt;&lt;/p&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My problem is that the values of the checkbox are not being stored in the array.  When i run this code and click submit i get a null pointer exception because the list in the source object is empty.  The sourceName works perfectly but the checkboxes are not actually adding anything.  </p>