Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to convert property value of type java.lang.String to required type double

Im having this error whenever I enter an empty string in the textbox and try to save it Im having this error:

Failed to convert property value of type java.lang.String to 
   required type double for property customerAcctSetting.maxAllowableAmount; 
nested exception is java.lang.IllegalArgumentException: Cannot convert value of 
    type [java.lang.String] to required type [double] for 
    property maxAllowableAmount:
PropertyEditor [bp.ar.util.NumberFormatUtil$CustomerDoubleEditor] returned
    inappropriate value

But when I enter an invalid number format such as "ddd" I have this error:

Failed to convert property value of type java.lang.String to required
    type double for property customerAcctSetting.maxAllowableAmount; 
nested exception is java.lang.NumberFormatException: For input string: "ddd"

I have this binder in my controller:

@InitBinder
public void initBinder(WebDataBinder binder) {
    NumberFormatUtil.registerDoubleFormat(binder);
}

And I have a class NumberFormatUtil.java that implements the static function registerDoubleFormat(binder):

NumberFormatUtil.java

public static void registerDoubleFormat (WebDataBinder binder) {
    binder.registerCustomEditor(Double.TYPE, new CustomerDoubleEditor());
}

private static class CustomerDoubleEditor extends PropertyEditorSupport{    
    public String getAsText() { 
        Double d = (Double) getValue(); 
        return d.toString(); 
    } 

    public void setAsText(String str) { 
        if( str == "" || str == null ) 
            setValue(0); 
        else 
            setValue(Double.parseDouble(str)); 
    } 
}

Im using Spring 3.0.1. Im very new to java and other related technologies such as spring. Please help. Thanks in advance.

like image 589
NinjaBoy Avatar asked May 28 '12 02:05

NinjaBoy


3 Answers

Change your setAsText() method likes here,

   public void setAsText(String str) { 
       if(str == null || str.trim().equals("")) {
           setValue(0d); // you want to return double
       } else {
           setValue(Double.parseDouble(str)); 
       }
  } 
like image 65
Sai Ye Yan Naing Aye Avatar answered Oct 12 '22 23:10

Sai Ye Yan Naing Aye


I don't know if this is the cause of your problem, but str == "" is a bug.

If you are testing to see if a String is empty, use str.isEmpty() or str.length() == 0 or even "".equals(str).

The == operator tests to see if the two strings are the same object. This doesn't do what you want, because there can be many different String instance in your running application that represent the same string. The empty string is no different to other strings in this respect.


Even if this is not the cause of your problem, you should fix this bug, and make a mental note to not use == to test strings. (Or at least, not unless you've taken special steps to ensure that it will always work ... which is beyond the scope of this Q&A.)

like image 29
Stephen C Avatar answered Oct 13 '22 00:10

Stephen C


As for the empty string, I suppose the problem is that your 0 is casted to Integer, not Double so you have to use postfix d : 0.0d;

As for NumberFormatException, I don't see any problem that converter could not convert it. If you want to have a custom message for convertion errors you should put that message to your message properties file following the semantics of DefaultMessageCodeResolver I think it will be something like typeMismatch.java.lang.Double = "invalid floating point number" and have a message source in your bean configuration

    <bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>exceptions</value><!--- that means you have exceptions.properties in your class path with the typeMismatch string specified above-->
        </list>
    </property>
    </bean>

Also the concept of Property Editors is outdated now, the new API with converters is the way to go, because spring does not create a heap of helper objects(property editors) for any property being edited with this approach.

like image 30
Boris Treukhov Avatar answered Oct 12 '22 22:10

Boris Treukhov