title: JAXB marshalling / unmarshalling NullpointerException dilemma
body: <p>I am using a <code>java.xml.bind</code>-annotated Bean to create an XML output format that <strong>omits collections <em>if</em> they are empty</strong>. For that purpose the respective Getter has to look like this:</p>
  
  <pre><code>@XmlElementWrapper(name = "titles")
  @XmlElement(name = "title")
  public List&lt;XmlTitle&gt; getTitles() {
      if (titles.size() == 0) {
          return null;
      }
      return titles;
  }
  </code></pre>
  
  <p>This works fine during marshaling. Sadly, I am getting a <code>NullpointerException</code> once I want to <strong>unmarshal the very same XML file</strong>:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:305)
      at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:269)
      at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:121)
      at com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:213)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:538)
      at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:107)
      at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:158)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
      at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
      at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
      at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:754)
      at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:735)
  </code></pre>
  
  <p>This has to do with namely that Getter returning <code>null</code>. As some JAXB expert explains on the <a href="https://java.net/projects/jaxb/lists/users/archive/2011-05/message/52" rel="noreferrer">Glassfish JAXB forum</a>:</p>
  
  <blockquote>
    <p>The JAXB unmarshaller does not expect setters for properties of type
    List; it'll just use x.getSomeList().add( y ) to add another child.</p>
  </blockquote>
  
  <p>How do I best solve this dilemma?</p>