How to ask BeanUtils to ignore null values

Posted by Calm Storm on Stack Overflow See other posts from Stack Overflow or by Calm Storm
Published on 2010-04-08T15:27:26Z Indexed on 2010/04/08 16:13 UTC
Read the original article Hit count: 447

Filed under:
|

Using Commons beanUtils I would like to know how to ask any converter say the Dateconverter to ignore null values and use null as default. As an example consider a public class,

public class X {
    private Date date1;
    private String string1;
    //add public getters and setters
}

and my convertertest as,

public class Apache {

    @Test
    public void testSimple() throws Exception {
        X x1 = new X(), x2 = new X();
        x1.setString1("X");
        x1.setDate1(null);
        org.apache.commons.beanutils.BeanUtils.copyProperties(x2, x1);
        //throws ConversionException
        System.out.println(x2.getString1());
        System.out.println(x2.getDate1());
    }
}

The above throws a NPE since the date happens to be null. This looks a very primitive scenario to me which should be handled by default (as in, I would expect x2 to have null value for date1). The doco tells me that I can ask the converter to do this. Can someone point me as to the best way for doing this ?

I dont want to get hold of the Converter and isUseDefault() to be true because then I have to do it for all Date, Enum and many other converters !

© Stack Overflow or respective owner

Related posts about java

Related posts about commons-beanutils