title: BeanUtils.copyProperties ignoring null values
body: <p>I have the following class:</p>
  
  <pre><code>import org.apache.commons.beanutils.BeanUtils;
  import com.thoughtworks.xstream.XStream;
  ...
  
  public class MyBean {
      protected static final XStream XSTREAM = new XStream(new DomDriver());
  
      protected String name;
      protected Something something;
  
      public MyBean() {
          something = new Something();
      }
  
      public MyBean(String xml) {
          this();
          MyBean beanFromXML = (MyBean) XSTREAM.fromXML(new StringReader(xml));
          BeanUtils.copyProperties(this, beanFromXML);
      }
  
      public String toString() {
          return XSTREAM.toXML(this);
      }
  
      // Getters and setters...
  }
  </code></pre>
  
  <p>It's a bean with ability to serialize and deserialize to/from XML using XStream.</p>
  
  <p>I also added a non-args constructor that initializes <code>something</code>, to avoid null pointer errors - the bean is actually a lot more complex, and I don't want to be checking "is <code>something != null</code>?" a million times.</p>
  
  <p>The problem arises when I use the XML-constructor. Lets say I've the following XML:</p>
  
  <pre><code>&lt;myBean&gt;
      &lt;name&gt;John&lt;/name&gt;
  &lt;/myBean&gt;
  </code></pre>
  
  <p>This is what I would like the constructor to do:</p>
  
  <pre><code>name: "John";
  something: new Something();
  </code></pre>
  
  <p>However, since there is no <code>&lt;something&gt;</code> element in the XML, BeanUtils.copyProperties makes <code>something = null;</code>, thus what I get is:</p>
  
  <pre><code> name: "John"
   something: null
  </code></pre>
  
  <p>How can I copy <code>beanFromXML</code>'s properties into <code>this</code>... but ignoring the null properties instead of overwriting them?</p>