Page 1 of 1

Docx to HTML - NullPointerException when adding table

PostPosted: Fri Jan 09, 2015 10:30 pm
by Ismael90
Hello,

First of all, thank you for docx4j as it is an amazing tool!

I am trying to copy a section of a docx and export it to an html document. Images, headings and lists are working well, but when I try to add a simple table (with no borders and no format), I receive the following error:

Code: Select all
Caused by: java.lang.NullPointerException
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.getCellPStyle(ParagraphStylesInTableFix.java:250)
   at org.docx4j.convert.out.common.preprocess.ParagraphStylesInTableFix$StyleRenamer.apply(ParagraphStylesInTableFix.java:555)
   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)


And this is part of the code:

Code: Select all
List<Object> listOfTables= getAllElementFromObject(wordMLPackage.getMainDocumentPart(), Tbl.class);
wordMLPackage2.getMainDocumentPart().addObject(listOfTables.get((0)));

HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(htmlmediafolder);
htmlSettings.setImageTargetUri(htmlmediafolder);
htmlSettings.setWmlPackage(wordMLPackage2);
                   
// For testing purposes
wordMLPackage2.save(new java.io.File(outputfolder + "/HelloWord.docx"));
                   
OutputStream os = new FileOutputStream(outputfolder + "/sampleout.html");
[b]Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);[/b]



The "HelloWord.docx" document is working OK. Any ideas?

Thanks in advance,
Ismael

Re: Docx to HTML - NullPointerException when adding table

PostPosted: Fri Jan 09, 2015 10:53 pm
by jason
Looking at the code, it suggests you have a paragraph (in a table cell) which tries to use a style which isn't defined in the styles part.

I guess that's plausible based on the code you posted. You should be able to copy the existing styles part over to your wordMLPackage2

As a general comment, you'd be better off cloning your original wordMLPackage, then deleting the bits you don't want. That way you can be sure all required parts (and styles etc) are present.

(Alternatively, of course, there is MergeDocx)

Re: Docx to HTML - NullPointerException when adding table

PostPosted: Tue Jan 13, 2015 4:47 am
by Ismael90
Problem solved, thanks! :)

Re: Docx to HTML - NullPointerException when adding table

PostPosted: Thu Feb 05, 2015 9:39 pm
by Ismael90
The solution was simply to change getAllElementFromObject():

Code: Select all
   
          private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
        List<Object> result = new ArrayList<Object>();
        if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
      
        if (obj.getClass().equals(toSearch)) {
         result.add(obj);
        }else if (obj.getClass().equals(Tbl.class)) {
           result.add(obj);
        } else if (obj instanceof ContentAccessor) {
         List<?> children = ((ContentAccessor) obj).getContent();
         for (Object child : children) {
          result.addAll(getAllElementFromObject(child, toSearch));
         }
      
        }
        return result;
   }


Before, I cloned the original wordMLPackage as Jason said.
Maybe it can help someone!

Cheers,
Ismael