commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niall Pemberton <niall.pember...@gmail.com>
Subject Re: Setting null on Integer property via BeanUtils.setProperty
Date Wed, 09 Dec 2009 11:18:20 GMT
On Wed, Dec 9, 2009 at 10:06 AM, Syed Muhammad Humayun
<smhumayun@iblgrp.com> wrote:
> Hi,
>
> How can I set a 'null' on Integer property (or any other wrappers like
> Double, Long, Character etc) via BeanUtils.setProperty? If its not possible
> this way than is there any other way using apache commons ?

If you don't want "conversion" then you can use
org.apache.commons.beanutils.PropertyUtils.setProperty() method - but
the "value" object has to be the correct type (or null).

For PropertyUtils see: http://tinyurl.com/yf9f6wk

BeanUtils adds conversion to PropertyUtils  - it tries to convert the
value you specified to the correct type of the the bean's property.
These converters have behaviour defined on how to handle "null" values
and operate in two modes:
 - throw an exception
 - use a default value

The default set of converters which are "registered" with BeanUtils
have a default value specified - so for example the converter
registered for Integer types has a default value of zero. This is why
you can't set a "null" value at the moment. If you want null values to
be set then you need to register converter implementations for those
types with a default value of null. So for example you would do
something like...

  ConvertUtils.register(new IntegerConverter(null), Integer.class);
  ConvertUtils.register(new DoubleConverter(null), Double.class);

(Note: the "null" value in the constructors is the default value)

For ConvertUtils see: http://tinyurl.com/ydhq85s
For converters see: http://tinyurl.com/yl2pl2q

Niall

> Regards,

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message