[groovy-user] unable to cast '13' to int

19 views
Skip to first unread message

Ravi

unread,
Jul 23, 2012, 8:44:49 PM7/23/12
to us...@groovy.codehaus.org

I am getting following exception. Any idea why it is unable to convert?

thanks
Ravi



org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
cast object '13' with class 'java.lang.String' to class 'int'
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:143)
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.intUnbox(DefaultTypeTransformation.java:60)
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:295)
at
org.codehaus.groovy.reflection.CachedField.setProperty(CachedField.java:65)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2348)
at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3312)
at groovy.lang.MetaClassImpl.setProperties(MetaClassImpl.java:1477)
at
org.codehaus.groovy.runtime.callsite.ConstructorSite$NoParamSite.callConstructor(ConstructorSite.java:122)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:54)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


David Clark

unread,
Jul 23, 2012, 9:07:53 PM7/23/12
to us...@groovy.codehaus.org
'13'.toInteger() will work.

Ravi

unread,
Jul 24, 2012, 12:15:15 AM7/24/12
to us...@groovy.codehaus.org, David Clark

To write code to convert to int means I have to do custom processing I
will have to keep track of which values are integer / float / boolean
and so on. It is much more complicated that straight variable assignment.

I was wondering why DefaultTypeTransformation.castToNumber was unable to
do so. The same code was working in Grails, but I stripped out grails
and using only Spring+Groovy and it doesn't work.


thanks
Ravi

Jochen Theodorou

unread,
Jul 24, 2012, 3:34:36 AM7/24/12
to us...@groovy.codehaus.org
Am 24.07.2012 06:15, schrieb Ravi:
>
> To write code to convert to int means I have to do custom processing I
> will have to keep track of which values are integer / float / boolean
> and so on. It is much more complicated that straight variable assignment.
>
> I was wondering why DefaultTypeTransformation.castToNumber was unable to
> do so. The same code was working in Grails, but I stripped out grails
> and using only Spring+Groovy and it doesn't work.

the automatic conversion to int works only for 1 char strings, and then
it gives the index, not the number. '13' is a string, so automated
conversion does not work. You can use
NumberFormat.numberInstance.parse('13') as a more general solution (not
for boolean though). That version also allows you to use a locale. You
may or may not know, that parsing numbers is actually far from a simple
matter. In Germany we use "." to seperate thousands and "," for the
fraction, while in the US it is often the other way around. There are
many such oddities.

Bye blackdrag
Reply all
Reply to author
Forward
0 new messages