title: Why am I getting null object after submitting form in Spring?
body: <p>I am new to Spring and yesterday I created a simple app. I type book's title, author and genre and it saves it to <code>List&lt;Book&gt;</code>. Book.java contains private fields (title, author, genre). </p>
  
  <p>So creating books and saving them to list works fine. Also I can view all books I have added. So it works fine. But now I want to create and delete them. So I have BookService.java that can add and delete books.</p>
  
  <p><strong>BookService.java</strong></p>
  
  <pre><code>private List&lt;Book&gt; allBooks = new ArrayList&lt;&gt;();
  
  public List&lt;Book&gt; getAllBooks() {
      return allBooks;
  }
  
  public void addBook(String title, String author, String genre) {
      allBooks.add(new Book(title, author, genre));
  }
  
  public void deleteBook(Book book) {
      allBooks.remove(book);
  }
  </code></pre>
  
  <p>This is stuff in my controller to delete books</p>
  
  <pre><code>@GetMapping("/books/delete")
  public String deleteBook(Model model) {
      model.addAttribute("BookList", bookService.getAllBooks()); // Works fine
      return "delete";
  }
  
  @PostMapping("/books/delete")
  public String deletedBook(@ModelAttribute Book book, Model model) {
      System.out.println(book.getTitle()); // null
  
      bookService.deleteBook(book); // can't delete null so nothing happens to the list
      model.addAttribute("deletedBook", book);
      return "deletedBookResult";
  }
  </code></pre>
  
  <p><strong>delete.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"/&gt;
    &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h2&gt;Delete page&lt;/h2&gt;
  
  &lt;div th:each="bookObj : ${BookList}"&gt; &lt;!-- BookList - all books I add using submit form --&gt;
    &lt;form action="#" th:action="@{/books/delete}" th:object="${bookObj}" method="post"&gt; &lt;!-- I SEND bookObj --&gt;
      &lt;input type="submit" th:value="${bookObj.title}"/&gt; &lt;!-- WORKS. I GET BOOK'S NAME ON THIS BUTTON--&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I use <strong>th:each="bookObj : ${BookList}"</strong>. So bookObj is every book I add. That's why I use <strong>th:object=${bookObj}</strong>. There is a <strong>form</strong> for each book I added later. And it display's it's title on the button. But when I press it, I get null to IDEA's console and on webpage. Why?
  Thank you in advance.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en" xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"/&gt;
    &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h2 th:text=" 'You have just deleted' + ${deletedBook.title}"&gt;&lt;/h2&gt;
  &lt;!-- You have just deleted null --&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>