简体   繁体   中英

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

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>

The error message tells you it can't convert an empty input String "" to a long value.

Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: ""

There are two long parameters in the method call, codeCompte and codeCompte2 . This means these values have to be valid long values, but according to the error message, one of them was an empty String (ie no value) instead.

It looks like codeCompte is generated by your application, so if this is where the empty value is, there's probably some error in your code.

But more likely the problem is <input type="text" name="codeCompte2" /> . You should somehow check in your application if the value entered in this input field is a valid number. Check out the input type "number" , which would probably be better suited for this input field.

when you use input ( codeCompte or codeCompte2 ) you don't have value ,so post field is "","" default type is String

springmvc can't convert value of type java.lang.String to required type long

so :

  1. you should be have default value 0

  2. you can change AgentController.saveOperation method field (codeCompte and codeCompte2) type to String ,then catch in the method

you need to add default value for codeCompte2

in agent.html

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM