mercredi 25 mars 2020

NullPointerException because of empty spring form

I'm getting NPE because I'm sending empty form using spring forms. Then building object using @ModelAttribute.

Sounds stupid I know. But I want to allow null values and set default values in java code.

Interesting thing that there was no problem with empty fields in my previous projects.

So I just can't get what is going wrong.

So main questions:

  • Why I can't send empty values?

  • How to allow nulls?

  • And what is the best way to set default value if received input field is null?

Here's code

Just simple DTO without any @NotNull or something like this

public class EquationDto {

    private Double a;   
    private Double b;
    private Double c;

    public EquationDto() {      
    }

    public Double getA() {
        return a;
    }
    public void setA(Double a) {
        this.a = a;
    }
    public Double getB() {
        return b;
    }
    public void setB(Double b) {
        this.b = b;
    }
    public Double getC() {
        return c;
    }
    public void setC(Double c) {
        this.c = c;
    }
}

Controller

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showHome(Model model) {
        model.addAttribute("equation", new EquationDto());
        return "index";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String equationForm(@ModelAttribute("equation") EquationDto equationDto, Model model) {

        if (equationDto.getA() == 0 || equationDto.getA() == null) {
            model.addAttribute("error", "Property a cannot be empty or zero");      
            return "index";
        }
        if(equationDto.getB() == 0 || equationDto.getB() == null)
            equationDto.setB(1.0);
        if(equationDto.getC() == null) 
            equationDto.setC(0.0);

        equationService.equationCulc(equationDto);

        return "redirect:/examples";
    }

Form

<form:form action="${pageContext.request.contextPath}/" method="post" modelAttribute="equation">
        <div class="form-group row">
            <div class="form-row">
                <div class="col">
                    <form:input type="text" path="a" name="a" class="form-control form-control-sm"/>
                </div>
                <div class="col">
                    <span><i>x<sup>2</sup> +</i></span>
                </div>
                <div class="col">
                    <form:input type="text" path="b" name="b" class="form-control form-control-sm"/>        
                </div>
                <div class="col">
                    <span><i>x +</i></span>
                </div>
                <div class="col">
                    <form:input type="text" path="c" name="c" class="form-control form-control-sm"/>
                </div>
                <div class="col">
                    <span><i> = 0</i></span>
                </div>
            </div>
            <input type="submit" class="btn btn-secondary mx-auto mt-4" value="Сalculate">
        </div>
    </form:form>



Aucun commentaire:

Enregistrer un commentaire