On Fri, Oct 3, 2008 at 7:30 PM, Tom Spencer <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm new to BeanUtils and I would firstly like to say a big thank you - this 
> is such a useful utility and has saved me countless hours of my time!
>
> I am, however, running into a difficulty. I am using BeanUtils 1.8.0 to copy 
> a java.util.Date property from one bean to another. I have noticed that if 
> the java.util.Date property is set to null, when I call 
> BeanUtils.copyProperties() I get the following error:
>
> Exception in thread "main" org.apache.commons.beanutils.ConversionException: 
> No value specified for 'Date'
>    at 
> org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:310)
>    at 
> org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:136)
>    at 
> org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:60)
>    at 
> org.apache.commons.beanutils.BeanUtilsBean.convert(BeanUtilsBean.java:1074)
>    at 
> org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:437)
>    at 
> org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:286)
>    at 
> org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:137)
>    at Main.main(Main.java:10)
>
>
> I have copied the sample code for the two beans and a test main class below. 
> Could anybody else verify that this is occurring too? I am using 
> beanutils-1.8.0.

You have a choice of two flavours for copying properties in BeanUtils:
1) Usng PropertyUtils copyProperties() method[1] - this just copies
any properties unchanged
2) Using BeanUtils  copyProperties() method[2] - copies AND converts
properties - so for example, if theres a property called "foo" of type
String in the source and a property called "foo" of type Integer in
the destination then it tries to convert the String to an Integer
using the Converter registered for Integers.

If you don't need conversion then you can switch to using
PropertyUtils. If however you have some conversion needs, but you
don't want it to throw an exception when a value is "null" (which is
the default behaviour) - then you can configure a converter for Date
which has a default value. So for example, if you wanted a default of
"null" for Dates, then something like the following:

java.util.Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, java.util.Date.class);

Then when you call BeanUtils.copyProperties() it should no longer
throw an exception.

Niall

[1] http://tinyurl.com/4rc5ho
[2] http://tinyurl.com/44csva


> Tom
>
> -------------------------------------
>
> import java.util.Date;
>
> public class Bean1 {
>
>    private Date date;
>
>    public Bean1() {
>        super();
>    }
>
>    public Date getDate() {
>        return date;
>    }
>
>    public void setDate(Date date) {
>        this.date = date;
>    }
>
> }
>
> -------------------------------------
>
> import java.util.Date;
>
> public class Bean2 {
>
>    private Date date;
>
>    public Bean2() {
>        super();
>    }
>
>    public Date getDate() {
>        return date;
>    }
>
>    public void setDate(Date date) {
>        this.date = date;
>    }
>
> }
>
> -------------------------------------
>
> import org.apache.commons.beanutils.BeanUtils;
>
> public class Main {
>
>    public static void main(String[] args) throws Exception {
>        Bean1 bean1 = new Bean1();
>        bean1.setDate(null);
>        Bean2 bean2 = new Bean2();
>
>        BeanUtils.copyProperties(bean2, bean1);
>
>        System.out.println(bean2.getDate());
>
>    }
>
> }
>
>
>
>
>
> _________________________________________________________________
> Discover Bird's Eye View now with Multimap from Live Search
> http://clk.atdmt.com/UKM/go/111354026/direct/01/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to