commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jesse Vitrone" <>
Subject RE: BeanUtils.populate problem
Date Thu, 17 Apr 2003 17:04:14 GMT
Yes?  Did you mean to say something about this?

-----Original Message-----
From: Damaraju Nagendra Kumar [] 
Sent: Thursday, April 17, 2003 8:42 AM
To: 'Jakarta Commons Users List'
Subject: RE: BeanUtils.populate problem

-----Original Message-----
From: Jesse Vitrone []
Sent: Wednesday, April 16, 2003 8:18 PM
Subject: BeanUtils.populate problem

I posted something along these lines earlier, but it doesn't really get to
the heart of the problem, so I'm posting something a little more specific.
Here's an example of the problem I'm running into:
bean - a bean with some Object on it, like a Person.  In this case, person
on the bean is null.
on my properties, I have a "patient.firstName" that has a String value.
When I call
    BeanUtils.populate(bean, properties);
if my person is null on the bean, then when populate tries to populate it,
it dies, because it winds up trying to call setFirstName on a null person.
I would prefer if Commons would just create me a new Person if it needs to
set something on it, but it's null.
If I add this to PropertyUtils.getSimpleProperty:
        This existed already:
        // Call the property getter and return the value
        Object value = readMethod.invoke(bean, new Object[0]);
        Below it, I added:
        if (value == null) {
            try {
                Method writeMethod = getWriteMethod(descriptor);
                if (writeMethod == null) {
                    throw new NoSuchMethodException("Property '" + name +
                            "' has no setter method");
                value = readMethod.getReturnType().newInstance();
                writeMethod.invoke(bean, new Object[] {value});
            } catch (Exception e) {
This works just like I want it to :)
Do you think people would find something like this useful?  I know I would.
I don't want all the objects on my bean (and the objects on that object,
etc) to have to be initialized already when I try to set them.
Does anyone else think that this should be the default behavior?  Or, even
if it's not the default, maybe just be able to pass a flag to force an init
of a null object?
Let me know what you think.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message