views:

394

answers:

3

The following code when executed on certain machines in our company causes an IllegalArgumentException to be thrown:

Color sludge = new Color(133, 133, 78);
//throws IAE with message "Color parameter outside of expected range: Red Green Blue"

An equivalent call using float arguments instead works:

Color sludge = new Color(0.522, 0.522, 0.306); // 133/255 = 0.522, 78/255 = 0.306

Why might this be the case? And why would it only affect certain machines?

Might it have something to do with the fact that these Color objects are defined in Spring like this:

<bean id="sludge" class="java.awt.Color">
    <constructor-arg value="133"/>
    <constructor-arg value="133"/>
    <constructor-arg value="78"/>
</bean>
+3  A: 

I'm NOT an expert with spring. but did you tried to set the type to int ?

<constructor-arg type="int" value="133">

?

Pierre
+1 Not sure if that'll make a difference, but I like the way you're thinking
skaffman
Nice idea, but that doesn't seem to make any difference :(
butterchicken
And did you try to add a <value/> tag ? <constructor-arg type="int"><value>133</value></constructor-arg> ?
Pierre
@Pierre - I was being a spoon; your first suggestion did the business
butterchicken
Spring will automagically convert to correct type here
Paul McKenzie
@Paul McKenzie - but Spring uses precedence in a sorted list of valid constructors, and both the int and float constructors are valid based upon the call in my q. How would it know to pick the right one without a nudge?
butterchicken
A: 

well if you read those settings from a file or something and do not particularly know the types, they might be strings where you think they are integers? (should be a comment to the first answer aswell.)

Hurix
Spring will resolve available constructors - there is no such thing as a `String` constructor on Color. Pierre suggested an int/float issue, but that doesn't seem to have made any difference.
butterchicken
+4  A: 

being more pedant:

<bean id="sludge" class="java.awt.Color">
    <constructor-arg index="0" type="int"><value>133</value></constructor-arg>
    <constructor-arg index="1" type="int"><value>133</value></constructor-arg>
    <constructor-arg index="2" type="int"><value>78</value></constructor-arg>
</bean>

EDIT

check also this blog post

dfa
+1 for the fantastic blog post
butterchicken
I fully agree, that blog post is both thorough and clear.
extraneon