title: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id'
body: <p>I have a very simple spring boot application where I am trying to save an updated location to the database. But whenever I am retrieving a list of saved locations and saving the updated location to the DB, I am getting below error:</p>
  
  <pre><code>2019-11-27 09:29:05.330  WARN 14764 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
  Field error in object 'location' on field 'id': rejected value []; codes [typeMismatch.location.id,typeMismatch.id,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [location.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: ""]]
  </code></pre>
  
  <p>Below are my classes:</p>
  
  <p>LocationRepository.java:</p>
  
  <pre><code>package com.springBoot.location.controllers;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import com.springBoot.location.entities.location;
  import com.springBoot.location.service.LocationService;
  
  @Controller
  public class LocationController {
  
      @Autowired
      private LocationService service;
  
      @RequestMapping("/showCreate")
      public String showCreate() {
          return "createLocation";
      }
  
      //when the form in "createLocation.jsp" will be submitted, the same page "createLocation.jsp" will be returned, 
      @RequestMapping("/saveLoc")                                 //but this time, with a message, stating that the location has been saved.
      public String saveLocation(@ModelAttribute("location")location location,ModelMap modelmap) {//when the page will be saved, a "location" bean/object will be created
          location saveLocation = service.saveLocation(location);            //all the fields from that page will be saved into that object and
          String msg = "location saved with id "+saveLocation.getId();      //will be sent as request to the database
          modelmap.addAttribute("msg",msg);//modelMap is for handling responses from the db.
          return "createLocation";                                           
      } 
  
      @RequestMapping("/displayLocations")
      public String displayLocations(ModelMap modelMap) {
          List&lt;location&gt; allLocations = service.getAllLocations();
          modelMap.addAttribute("locations",allLocations);
          return "displayLocations";
      }
  
      @RequestMapping("/addLoc")
      public String showCreate1() {
          return "createLocation";
      }
  
      @RequestMapping("/getID")
      public String getId(@ModelAttribute("location")location location,ModelMap modelMap) {
          List&lt;location&gt; allLocations = service.getAllLocations();
          location Lastlocation = allLocations.get(allLocations.size()-1);
          int id = Lastlocation.getId();
          String lastId = "The last ID was "+id;
          modelMap.addAttribute("lastId",lastId);
          return "createLocation";
  
      }
  
      @RequestMapping("/deleteLocation")
      public String deleteLocation(@RequestParam("id")int id,ModelMap modelMap) {
          location l = service.getLocationById(id);
          service.deleteLocation(l);
          List&lt;location&gt; allLocations = service.getAllLocations();
          modelMap.addAttribute("locations",allLocations);
          return "displayLocations";
      }
  
      @RequestMapping("/updateLocation")
      public String updateLocation(@RequestParam("id")int id,ModelMap modelMap) { 
          location l = service.getLocationById(id);
          modelMap.addAttribute("locations",l);
          return "updateLocation";
  
      }
  
      @RequestMapping("/updateLoc")
      public String saveUpdateLocation(@ModelAttribute("location")location location,ModelMap modelMap) {
          service.updateLocation(location);
          List&lt;location&gt; locations = service.getAllLocations();
          modelMap.addAttribute("locations",locations);
          return "displayLocations";
      }
  
  
  
  
  }
  </code></pre>
  
  <p>Location.java</p>
  
  <pre><code>package com.bharath.location.entities;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Location {
  
      @Id
      private int id;
      private String code;
      private String name;
      private String type;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getCode() {
          return code;
      }
  
      public void setCode(String code) {
          this.code = code;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getType() {
          return type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
      @Override
      public String toString() {
          return "Location [id=" + id + ", code=" + code + ", name=" + name + ", type=" + type + "]";
      }
  
  }
  </code></pre>
  
  <p>Below are my jsp files:</p>
  
  <p>createLocation.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Create location&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;form action="saveLoc" method="post"&gt; &lt;!-- value of the "action" attribute is same as the value inside "request controller" method name, to which the method maps to --&gt; 
   &lt;pre&gt;
      Id:&lt;input type="text" name="id"/&gt;&lt;!-- the value in the "name" attribute should be same as the variable names in the "entity" class --&gt;
      &lt;a href="getID"&gt;Want to know the last ID ?&lt;/a&gt;
      Code:&lt;input type="text" name="code"/&gt;
      Name:&lt;input type="text" name="name"/&gt;
      Type: Urban&lt;input type="radio" name="type" value="URBAN"/&gt;&lt;!-- when the page will be saved, the value will be saved as "URBAN" --&gt;
            Rural&lt;input type="radio" name="type" value="RURAL"/&gt;
       &lt;input type="submit" value="save"/&gt;
   &lt;/pre&gt;
   &lt;/form&gt;
  ${lastId}&lt;br&gt;
  ${msg}&lt;br&gt;
   &lt;a href="displayLocations"&gt;View all&lt;/a&gt; &lt;!-- the value in the "href" attribute will be inside the "RequestMapping" 
  &lt;/body&gt;                                                     annotation mapping to a specific class for handling a request --&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>displayLocations.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!-- in order to use JSTL, we first mentioned the jstl dependency in the pom.xml and then used taglib directive. We use a prefix so that --&gt;
  &lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;&lt;!-- we dont have to use the full uri everywhere --&gt;
  &lt;%@page isELIgnored="false" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;List of locations&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;ID&lt;/th&gt;
              &lt;th&gt;Code&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Type&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;c:forEach items="${locations}" var="location"&gt;&lt;!-- the value in the "items" attribute is same as the "key" value in the modelMap, in the respective java class --&gt;
          &lt;tr&gt;
              &lt;td&gt;${location.id}&lt;/td&gt;
              &lt;td&gt;${location.code}&lt;/td&gt;
              &lt;td&gt;${location.name}&lt;/td&gt;
              &lt;td&gt;${location.type}&lt;/td&gt;
              &lt;td&gt;&lt;a href="deleteLocation?id=${location.id}"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;!--"?id=${location.id}" will pass the id dynamically to the controller --&gt;
              &lt;td&gt;&lt;a href="updateLocation?id=${location.id}"&gt;Update&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;  
          &lt;/c:forEach&gt;
      &lt;/table&gt;
      &lt;br&gt;
  &lt;a href="addLoc"&gt;Add more locations&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>updateLocation.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@page isELIgnored="false"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Create Location&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form action="updateLoc" method="post"&gt;
  &lt;pre&gt;
  Id: &lt;input type="text" name="id" value="${location.id}" readonly/&gt;
  Code: &lt;input type="text" name="code" value="${location.code}"/&gt;
  Name: &lt;input type="text" name="name" value="${location.name}"/&gt;
  Type: Urban &lt;input type="radio" name="type" value="URBAN" ${location.type=='URBAN'?'checked':''}/&gt;
      Rural &lt;input type="radio" name="type" value="RURAL" ${location.type=='RURAL'?'checked':''}/&gt;
  Email: &lt;input type="text" name="email" value="${location.name}"/&gt;
  Phone: &lt;input type="text" name="phone" value="${location.name}"/&gt;
  Address: &lt;input type="text" name="address" value="${location.name}"/&gt;
  &lt;input type="submit" value="save"/&gt;
  &lt;/pre&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>