2
votes

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.

2
perhaps 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? - RAZ_Muh_Taz
@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. - Ashley
so 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? - RAZ_Muh_Taz
yep - tried it multiple times, checked the database and the input is going into the right column in this case being grade. - Ashley
ok 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

2 Answers

3
votes

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 in Grade class into something different (for example value).
0
votes

The value '2.1' is an invalid long value. You can check the Max and Min value of corresponding data type as follows :

String s="2.1";     
System.out.println(Long.MAX_VALUE);//9223372036854775807
System.out.println(Double.MAX_VALUE);//1.7976931348623157E308

So either change the data type(Use Double or String) of the corresponding attribute of the entity OR do a front-end validation to restrict the input type.