Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 48916 invoked from network); 17 Apr 2003 12:43:27 -0000 Received: from lmg.affinity.com (HELO lmg04.affinity.com) (207.150.192.13) by daedalus.apache.org with SMTP; 17 Apr 2003 12:43:27 -0000 Received: from andhra ([202.63.106.2]) by lmg.ahnet.net with SMTP id <291916-26914>; Thu, 17 Apr 2003 05:42:31 -0700 Reply-To: From: "Damaraju Nagendra Kumar" To: "'Jakarta Commons Users List'" Subject: RE: BeanUtils.populate problem Date: Thu, 17 Apr 2003 18:12:28 +0530 Message-ID: <001001c304de$cf03f620$30bfa8c0@andhra> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) In-Reply-To: <001b01c30427$3d3624a0$306a640a@vitalworks.com> X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Importance: Normal Disposition-Notification-To: "Damaraju Nagendra Kumar" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N -----Original Message----- From: Jesse Vitrone [mailto:jessevitrone@vitalworks.com] Sent: Wednesday, April 16, 2003 8:18 PM To: commons-user@jakarta.apache.org 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) { e.printStackTrace(); } } 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. Jesse