java - Null Pointer Exception while unmarshalling XML with @XmlAnyElement annotation -


i'm trying unmarshal xml java object using jaxb , i'm getting strange null pointer exception. issue while unmarshalling. can marshal these classes fine. here relevant pieces of code (irrelevant portions denoted "..."):

  1. the jaxb root element:

    ... @xmlrootelement(name="assets") public class assets {     string product;     images images = new images();      ...      public assets() {}      ...      public string getproduct() { return this.product; }      @xmlattribute(name="product")     public void setproduct(string newproduct) { this.product = newproduct; }      public images getimages() { return this.images; }      @xmlelement(name="images")     public void setimages(images newimages) { this.images = newimages; }      ... } 
  2. the images sub-element of root element:

    ... @xmlrootelement(name="images") @xmlseealso(image.class) @xmltype(proporder = {"mainasset", "image"}) public class images {     list<image> images;     image mainasset;      private static char counter = 'a';     private static final string prefix = "product-image-";      // here's part causes npe     @xmlanyelement(lax=true)     public list<jaxbelement<image>> getimage() {         final list<jaxbelement<image>> imagelist = new arraylist<jaxbelement<image>>();         (final image g : this.images) {             imagelist.add(new jaxbelement(new qname(prefix + counter++), image.class, g));         }         counter = 'a';         return imagelist;     }      @xmlelement(name="renditions")     public void setimage(list<image> newimages) { this.images = newimages; }      public image getmainasset() { return this.mainasset; }      @xmlelement(name="main-asset-name")     public void setmainasset(image newmainasset) { this.mainasset = newmainasset; } } 
  3. the logic unmarshalling xml:

    ... public void modifyxml() { try {     jaxbcontext context = jaxbcontext.newinstance(assets.class, images.class, image.class);     unmarshaller um = context.createunmarshaller();     file f = new file("/path/to/my.xml");     assets assets = (assets) um.unmarshal(f);     ... } catch (jaxbexception e) {     e.printstacktrace(); } } ... 
  4. finally, xml i'm trying unmarshal (it might know xml file generated using jaxb marshaller, runs without problems):

    <?xml version="1.0" encoding="utf-8" standalone="yes"?> <assets product="test product">     <images>         <main-asset-name>             <path>stuff.jpg</path>             <title>thetitle</path>             <alt>thealt</path>             <description>thedescription</description>         </main-asset-name>         <product-image-a>             <path>48x48.jpg</path>             <title>thetitle</path>             <alt>thealt</path>             <description>thedescription</description>         </product-image-a>         <product-image-b>             <path>140x140.jpg</path>             <title>thetitle</path>             <alt>thealt</path>             <description>thedescription</description>         </product-image-b>         <product-image-c>             <path>1280x1280.jpg</path>             <title>thetitle</path>             <alt>thealt</path>             <description>thedescription</description>         </product-image-c>         <product-image-d>             <path>319x319.jpg</path>             <title>thetitle</path>             <alt>thealt</path>             <description>thedescription</description>         </product-image-d>     </images> </assets> 

okay, that's relevant code (i think). when run program, following error right after invoking unmarshaller:

java.lang.nullpointerexception     @ com.my.companys.name.images.getimage(images.java:25) 

and line number referenced line loop starts in images.java class file.

does have ideas why this.images might null here? appreciated. ahead of time.

here's solution i've been using now. it's bit of hack, hell... job.

first, raw xml string instead of reading file:

public void modifyxml() {     try {         jaxbcontext context = jaxbcontext.newinstance(assets.class, image.class);         unmarshaller um = context.createunmarshaller();         // xml should formatted in way jaxb can work         string rawxml = getrawxml("/path/to/my.xml");         // hand modified xml unmarshaller         assets umassets = (assets) um.unmarshal(new streamsource(new stringreader(rawxml)));         // stuff umassets         ...     } catch (exception e) {         e.printstacktrace();     } }  private string getrawxml(string path) throws ioexception {     string xml = readfile(path, standardcharsets.utf_8);     // should make jaxb can handle xml     xml = xml.replaceall("<main-asset-name>", "<image>");     xml = xml.replaceall("</main-asset-name>", "</image>");     xml = xml.replaceall("<product-image.*>", "<image>");     return xml.replaceall("</product-image.*>", "</image>"); }  private string readfile(string path, charset encoding) throws ioexception {     // simple file reader     byte[] encoded = files.readallbytes(paths.get(path));     return new string(encoded, encoding); } 

this seems trick, i'm going use now. of course, i'm still open better solutions if has one.


Comments