Page 1 of 1

diffx compare - NPE

PostPosted: Tue Sep 30, 2014 8:11 pm
by jignesh
Hi Jason,

Thank you very much for your suggestion. I have upgraded to v3,2.0 of dox4j and 3.2.1 of ImportXHTML. I had to make some changes in my code to make it work. It works perfectly fine now.
You are a star thanks for your help. :) :)

Now, I am trying to compare two generated documents and I am getting NullPointerException. I am trying to compare docs and have output in PDF format.

This is the code to compare documents
Code: Select all

   public void compareDocuments(String file1, String file2)
   {
      try
      {
         
         String newerfilepath = "D:/jignesh/documents_comparison_temp/"+file1+".docx";
         String olderfilepath = "D:/jignesh/documents_comparison_temp/"+file2+".docx";
         
         // 1. Load the Packages
         WordprocessingMLPackage newerPackage = WordprocessingMLPackage.load(new java.io.File(newerfilepath));
         WordprocessingMLPackage olderPackage = WordprocessingMLPackage.load(new java.io.File(olderfilepath));
         
         Body newerBody = ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).getBody();
         Body olderBody = ((Document)olderPackage.getMainDocumentPart().getJaxbElement()).getBody();
         
         System.out.println("Differencing..");
         LOGGER.info("Differencing..");
         
         // 2. Do the differencing
         java.io.StringWriter sw = new java.io.StringWriter();
         javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(sw);
         Calendar changeDate = null;
         Differencer pd = null;
         if (DIVIDE_AND_CONQUER)
         {
            Docx4jDriver.diff(XmlUtils.marshaltoW3CDomDocument(newerBody).getDocumentElement(), XmlUtils.marshaltoW3CDomDocument(olderBody).getDocumentElement(),sw);
         }
         else
         {
            pd = new Differencer();
            pd.setRelsDiffIdentifier("blagh");
            pd.diff(newerBody, olderBody, result, "someone", changeDate,newerPackage.getMainDocumentPart().getRelationshipsPart(),olderPackage.getMainDocumentPart().getRelationshipsPart());
         }
         
         // 3. Get the result
         String contentStr = sw.toString();
         System.out.println("Result: \n\n " + contentStr);
         LOGGER.info("Result: \n\n " + contentStr);
         Body newBody = (Body) XmlUtils.unwrap(XmlUtils.unmarshalString(contentStr));
         
         // 4. Display the result as a PDF
         // To do this, we'll replace the body in the newer document
         ((Document)newerPackage.getMainDocumentPart().getJaxbElement()).setBody(newBody);
         
         RelationshipsPart rp = newerPackage.getMainDocumentPart().getRelationshipsPart();
         handleRels(pd, rp);
         
         newerPackage.setFontMapper(new IdentityPlusMapper());
         boolean saveFO = false;

         
//FO exporter setup (required)
//.. the FOSettings object
         FOSettings foSettings = Docx4J.createFOSettings();
         if (saveFO)
         {
            foSettings.setFoDumpFile(new java.io.File("D:/jignesh/documents_comparison_temp/COMPARED..fo"));
         }
         foSettings.setWmlPackage(newerPackage);
               
         // exporter writes to an OutputStream.      
         OutputStream os = new java.io.FileOutputStream("D:/jignesh/documents_comparison_temp/COMPARED.pdf");
         //Don't care what type of exporter you use
         Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
                  
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
   
   private static void handleRels(Differencer pd, RelationshipsPart rp)
   {
      // Since we are going to add rels appropriate to the docs being
      // compared, for neatness and to avoid duplication
      // (duplication of internal part names is fatal in Word,
      //  and export xslt makes images internal, though it does avoid duplicating
      //  a part ),
      // remove any existing rels which point to images
      List<Relationship> relsToRemove = new ArrayList<Relationship>();
      for (Relationship r : rp.getRelationships().getRelationship() )
      {
         if (r.getType().equals(Namespaces.IMAGE))
         {
            relsToRemove.add(r);
         }
      }                  
      for (Relationship r : relsToRemove)
      {            
         rp.removeRelationship(r);
      }
      // Now add the rels we composed
      List<Relationship> newRels = pd.getComposedRels();
      for (Relationship nr : newRels)
      {                     
         rp.addRelationship(nr);
      }
   }



This is the exception I am getting
Code: Select all
ERROR org.docx4j.convert.out.common.AbstractExporter - Exception exporting package
java.lang.NullPointerException
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.getCellPStyle(ParagraphStylesInTableFix.java:291)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.apply(ParagraphStylesInTableFix.java:541)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:590)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:597)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:597)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.walkJAXBElements(ParagraphStylesInTableFix.java:597)
   at org.docx4j.TraversalUtil.<init>(TraversalUtil.java:209)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix.process(ParagraphStylesInTableFix.java:156)
   at org.docx4j.convert.out.common.Preprocess.process(Preprocess.java:179)
   at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:51)
   at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:32)
   at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:63)
   at org.docx4j.Docx4J.toFO(Docx4J.java:460)

I have also attached two word files that I am comparing. do you want to have a look at the code I am using to generate word documents?

I am not sure what I am doing wrong here. any input will be much appreciated.

Thanks a ton

Re: diffx compare - NPE

PostPosted: Sat Oct 04, 2014 12:38 pm
by jason
Using your documents with docx4j 3.2.1 and https://github.com/plutext/docx4j/blob/ ... ments.java worked for me.

Please try those.