Failed to convert type string to type long Spring Boot
It tries to bind grade
passed inside form data to controller method parameter:
You can either:
- add
@ModelAttribute
annotation to your controller method like:
@RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST)
public String newGrade(@ModelAttribute("form") Grade grade) {
gradeData.save(grade);
return "redirect:/listModules.html";
}
- change name of
grade
field inGrade
class into something different (for examplevalue
).
Related videos on Youtube
Ashley
Updated on June 04, 2022Comments
-
Ashley almost 2 years
I am trying to add a value of type double to my database using a form. However, when I try doing this I get an error stating:
Failed to convert value of type [java.lang.String] to required type [com.javainuse.model.Grade]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value '2.1'; nested exception is java.lang.NumberFormatException: For input string: "2.1"
If I add a whole number, however, the entry is added with no problem. Can anyone pinpoint where my error is? I've looked at similar questions but nothing seems to work :( My code is as follows:
Grade Controller:
@Controller public class GradeController { @Autowired private GradeRepository gradeData; @RequestMapping(value = "/addNewGrade.html", method = RequestMethod.POST) public String newGrade(Grade grade) { gradeData.save(grade); return ("redirect:/listModules.html"); } @RequestMapping(value = "/addNewGrade.html", method = RequestMethod.GET) public ModelAndView addNewGrade() { Grade grd = new Grade(); return new ModelAndView("newGrade", "form", grd); }
Grade repository:
package com.javainuse.data; import org.springframework.data.jpa.repository.JpaRepository; import com.javainuse.model.Grade; public interface GradeRepository extends JpaRepository<Grade, Long> { }
Add New Grade form:
<form name="form" action="" method="POST"> <div> <label>Grade </label> <input name="grade" /> </div> <div> <label>Module</label> <select name="moduleid"> <c:forEach items="${module.rows}" var="row"> <option value="${row.id}">${row.modulename}</option> </c:forEach> </select> </div> <div> <label>Student</label> <select name="studentid"> <c:forEach items="${student.rows}" var="row"> <option value="${row.id}">${row.firstname} ${row.lastname}</option> </c:forEach> </select> </div> <br> <input type="submit" value="Submit"/> </form>
Grade Model:
@Entity public class Grade { @GeneratedValue(strategy = GenerationType.AUTO) @Id private long id; private int moduleid; private int studentid; private double grade; public int getModuleid() { return moduleid; } public void setModuleid(int moduleid) { this.moduleid = moduleid; } public int getStudentid() { return studentid; } public void setStudentid(int studentid) { this.studentid = studentid; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } }
The datatype in my database is also double.
-
RAZ_Muh_Taz about 6 yearsperhaps the failure is that the value 2.1 is trying to be placed into the id variable of your Grade... are you sure that the particular exception is being thrown when trying to parse the double grade variable and not the long id variable?
-
Ashley about 6 years@RAZ_Muh_Taz Hi thank you for your reply. I thought that might be it, however when I add a number without the decimal point it is added the database with no error, so it can't be that.
-
RAZ_Muh_Taz about 6 yearsso instead of 2.1 you use a big obvious number like 9999, that value is placed into the database, is the value for the grade and not the id? you're sure?
-
Ashley about 6 yearsyep - tried it multiple times, checked the database and the input is going into the right column in this case being grade.
-
RAZ_Muh_Taz about 6 yearsok great, and you're 100% sure that the grade column for your database is of type double and not type Long?? sorry i don't know the framework but from the error it's clear that you are trying to parse a String into what your program/database thinks should be a Long and not a double which is what you want
-
RAZ_Muh_Taz about 6 yearsyou ahve GradeRepository extends JpaRepository<Grade, Long>, should that be a Double? just throwing stuff out there
-
Ashley about 6 yearsYes my grade column in the database is type double. I tried changing the JpaRespository to double but I am still receiving the same error :(
-
Ashley about 6 yearsExpected: class java.lang.Long, got class java.lang.Double;
-
-
Ashley about 6 yearsHi, grade is set to double (the only long value I have is id, which is not being added in the form)
-
Maciej Walkowiak about 6 yearsCan you post complete project on github?
-
Ashley about 6 yearsHi @Maciej, I am not familiar with github and therefore not sure how to use it. I am a beginner in spring boot and so the code posted is the only code I have with relation to the grade model.
-
Ashley about 6 yearshi, the data type is double - this is my issue. I am not sure why the error is showing an error of conversion string to long when I am actually entering a 'double' value type
-
Maciej Walkowiak about 6 yearsOk, change name of the field
grade
insideGrade
class tovalue
- it will work then as expected. -
DEBENDRA DHINDA about 6 yearsCan you please tell me the binded attribute. i.e. to which attribute the value is binding??
-
Ashley about 6 yearshi that doesn't work - the field grade inside the grade class is my table column
-
Maciej Walkowiak about 6 yearsYou can choose different column name than field using
@Column
from JPA. Anyway, I updated the answer with better solution than changing the field name.