Page 1 of 1

NPE in CustomXMLBinding with old JAXB 2.1.2

PostPosted: Mon Oct 18, 2010 4:49 am
by bharat007
Hello,

I have recently come across docx4j which by far is the best doc generation that I have found for java.
My setup includes:
docx4j-2.5.0
JAXB2_20100510.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.2.jar
jaxp-api-1.4.1.jar
jre1.5.0_11

While I have managed to create a sample docx file, I would like to use custom XML binding.
I am trying to run the sample CustomXMLBinding.java.It throws an exception which I have attached as txt file in "Exception.zip".

I can see the dynamic value that I inject through code in the customXml(item1.xml) but due to this exception, the main document.xml is empty which is because it is failing when trying applyBindings?

I have referred the previous forums regarding the NPE but the example I am using already has .toLowerCase() applied on itemId.

Thanks in advance.

Re: Exception running CustomXMLBinding(document.xml empty!!)

PostPosted: Mon Oct 18, 2010 5:10 am
by bharat007
BTW, I am trying to open the docx file using.load():
String inputfilepath = "C:/Files/DocGeneration/MedicalChartSample.docx";
String outputfilepath = "C:/Files/DocGeneration/MedicalChartSample-OUT.docx";
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));


Another look at the exception and it seems the exception is occurring when trying to load the docx file.
I have downloaded the docx file from ContentControl toolkit samples.

Re: Exception running CustomXMLBinding(document.xml empty!!)

PostPosted: Mon Oct 18, 2010 11:29 am
by jason
Code: Select all
java.lang.NullPointerException
   at org.docx4j.wml.Id.hashCode(Id.java:129)
   at java.util.HashMap.put(Unknown Source)
   at com.sun.xml.bind.v2.runtime.AssociationMap.addInner(AssociationMap.java:78)
   at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.recordInnerPeer(UnmarshallingContext.java:1025)
   at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:147)
   at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:402)
   at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:380)
   at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:35)
   at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
   at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:224)
:
   at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:83)
   at com.sun.xml.bind.v2.runtime.BinderImpl.associativeUnmarshal(BinderImpl.java:140)
   at com.sun.xml.bind.v2.runtime.BinderImpl.unmarshal(BinderImpl.java:111)
   at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.unmarshal(MainDocumentPart.java:249)


Hi, I can reproduce the error when I use JAXB 2.1.2 via

Code: Select all
-Djava.endorsed.dirs="c:\\JAXB\\2.1.2\\lib"


That's quite old, dating from 2007. Are you able to use a newer version of JAXB?

(You seem to have JAXB2_20100510.jar and jaxb-impl-2.1.2.jar. If JAXB2_20100510.jar is a new JAXB, can you remove the old jaxb-impl-2.1.2.jar from your path?)

My code for an sdt/content control id hashcode is:

Code: Select all
       public int hashCode() {
          return val.intValue();          
       }


Your NPE is occuring because BigInteger val is null - it looks like this is being triggered for each sdt in the document (even though they have proper ids)

I'm not sure what to do in this case. I could return some other int, but this might result in problems in downstream code which stores sdt in a hashmap. So I'm inclined to leave the code as is.

If you must use JAXB 2.1.2, some variation on

Code: Select all
       public int hashCode() {
          
          if (val==null) {
             System.out.println("** Null id val");
             return Math.abs(new java.util.Random().nextInt());             
          }
          return val.intValue();          
       }


allows CustomXmlBinding sample to run

Re: NPE in CustomXMLBinding with old JAXB 2.1.2

PostPosted: Fri Nov 05, 2010 12:28 am
by jason
I just got the same error with:

Code: Select all
IcedTea Runtime Environment (build 1.7.0-b21)
IcedTea 64-Bit Server VM (build 1.7.0-b21, mixed mode)


Evidently it uses an old JAXB.

The solution is the same:
Code: Select all
wget https://jaxb.dev.java.net/2.1.13/JAXB2_20100510.jar


and put the relevant bits in endorsed dir.