Results 1 to 4 of 4

Thread: Failed to convert property value of type [java.lang.String] to required type [Integer

  1. #1

    Default Failed to convert property value of type [java.lang.String] to required type [Integer

    I am getting below error for clientId property of type Integer in my MVC application:

    Code:
    Failed to convert property value of type [java.lang.String] to required type [java.lang.Integer] for property clientId; nested exception is java.lang.NumberFormatException: For input string: ""
    below is the initbinder code in the controller class:

    Code:
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
    
        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, false));
    
    }
    I understand that this error is coming because i am passing spaces in the input field for clientId which is of type Integer.

    but, what is the solution. one approach is to use client side javascript validation to check the values.. but is their any better approach to resolve the issue. thank you.

  2. #2

    Default

    now, the above issue is resoled by passing true instead of false in the registerCustomeEditor method of binder as follows:

    binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));

    but, giving similar error for alpha values set into the integer field.. is there a way to resolve it in the initbinder.

  3. #3
    Join Date
    Mar 2011
    Posts
    3

    Default Registering Integer

    Well, you can never reall y know what a user will post in the text field, soo instead of expecting an integer, how about accepting the string and parsing it checking for NumberFormatException?

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    If the numeric field is to be mandatory, you should use proper Spring form validation to enforce that something is written there and that this something is effectively a number. If the field is non-mandatory, then you should check in your controller if the value entered is null, empty string or a non number and in those cases ignore it, else convert it and use the integer value as you intend to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •