Java – Failed to convert value of type ‘java.lang.String’ to required type ‘long’; nested exception is java.lang.NumberFormatException: For input string: “”

javaspring-boot

I have this error but I don't have any idea where is the problem:

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Sep 02 12:57:36 CEST 2019
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: ""
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: ""

AgentController

@RequestMapping(value="/saveOperation", method=RequestMethod.POST)
    public String saveOperation(Model model, String typeOperation,long codeCompte,double montant, long codeCompte2){
        try {
            if(typeOperation.equals("VERS")){
                agentServices.verser(codeCompte, montant);
            }
            else if(typeOperation.equals("RETR")){
                agentServices.retirer(codeCompte, montant);
            } 
            else if (typeOperation.equals("VIR")){
                agentServices.virement(codeCompte,codeCompte2,montant);
            }
        }catch (Exception e){
            model.addAttribute("error",e);  
        }
        return "redirect:/consulterCompte?codeCompte="+codeCompte;
    }

agent.html

<form th:action="@{/saveOperation}" method="post">
<div>
<label>Compte: </label>
<input  type="hidden" name="codeCompte" th:value="${codeCompte}"/>
<label th:text="${codeCompte}"></label>
</div>
<div>
<input type="radio" name="typeOperation" value="VERS" checked="checked"  onchange="document.getElementById('forVirement').style.display='none'"/>
<label>Versement</label>
<input type="radio" name="typeOperation" value="RETR" onchange="document.getElementById('forVirement').style.display='none'"/>
<label>Retrait</label>
<input type="radio" name="typeOperation" value="VIR" onchange="document.getElementById('forVirement').style.display='block'"/>
<label>Virement</label>
</div>
<div id="forVirement" style="display:none">
<label>Transféré à:</label>
<input type="text" name="codeCompte2" />
</div>
<div>
<label>Montant:</label>
<input type="text" name="montant"/>
</div>
<button type="submit" class="btn btn-primary">OK</button>
</form>

Best Answer

you need to add default value for codeCompte2

in agent.html

<input type="text" name="codeCompte2" value="0" /> // add default value 
Related Topic