Java: XWPFWordExtractor.getText() 抛出 NullPointerException

Java: XWPFWordExtractor.getText() throws NullPointerException

我正在使用 Apache POI 创建一个包含以下代码的 .docx 文件:

XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(text);
String filePath = outputPathWithoutExtension + ".docx";
try {
    FileOutputStream stream = new FileOutputStream(new File(filePath));
    document.write(stream);
    stream.close();
} catch (IOException exception) {
    LOGGER.error("Could not create file '{}'", filePath);
}

然后我尝试用下面的代码阅读它:

FileInputStream fileStream = new FileInputStream(filePath);
try {
    XWPFDocument docx = new XWPFDocument(fileStream);
    XWPFWordExtractor wordExtractor = new XWPFWordExtractor(docx);
    text = wordExtractor.getText();
} catch (IOException | POIXMLException | OfficeXmlFileException
            | NullPointerException exception) {
     LOGGER.error("Could not load file - Exception: {}", exception.getMessage());
}

在我调用 getText() 的那一行,它抛出一个 NullPointerException:

java.lang.NullPointerException
at org.apache.poi.xwpf.extractor.XWPFWordExtractor.extractHeaders(XWPFWordExtractor.java:162)
at org.apache.poi.xwpf.extractor.XWPFWordExtractor.getText(XWPFWordExtractor.java:87)

问题似乎是 extractText 使用文档的 XWPFHeaderFooterPolicy 调用 extractHeaders ...在我的例子中是空的。当它试图在第一行使用它时...... boom.

我尝试创建自己的 "header/footer policy",如下所示:

try {
    new XWPFHeaderFooterPolicy(document);
} catch (IOException | XmlException exception) {
    LOGGER.warn("Could not create output document header - "
           + "document might not be readable in all readers");
}

但是,它本身会抛出 NullPointerException,因为它试图通过 doc.getDocument().getBody().getSectPr() 访问文档的 "SectPr",returns null ... 然后第一次使用那个... 砰。

所以,我的问题是:我显然没有正确创建 XWPFDocument ...有人可以纠正我吗?

旁注:如果我在 Word 中打开文件,文件看起来很好。如果在文件的创建和读取之间,我打开它、编辑它、保存它并关闭它,那么对 getText() 的调用将按预期执行,没有 NullPointerException。 Word 必须在保存时填写适当的 header/footer 策略。

啊哈!我在这里找到了答案:How to create a header/footer in new docx document?

我基本上只是太早放弃了一步。将此代码添加到文档的创建中使其可以读取:

// Add a SectPr and header/footer policy so document can be opened and read by POI
try {
    document.getDocument().getBody().addNewSectPr();
    new XWPFHeaderFooterPolicy(document);
} catch (IOException | XmlException exception) {
    LOGGER.warn("Could not create output document header - "
            + "document might not be readable in all readers");
}
    //---------------------Modify Footer or Headder change footer x headder 
//If you need some help tell me. agustinoscarmendez@gmail.com
try {
            XWPFDocument doc = new XWPFDocument(OPCPackage.open("C:\Users\amendez\Documents\NetBeansProjects\PuertasSRL_Presupuesto\Resources\Words y Excel Examples\Modelos de EPC.docx"));
            //XWPFDocument doc = new XWPFDocument();
            CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
            XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(doc, sectPr);
            XWPFHeaderFooterPolicy hfp = doc.getHeaderFooterPolicy();
            //XWPFFooter ffffff = hfp.getFooter(1); //Para cambiar el footer de la primera pagina
            XWPFFooter ffffff = hfp.getDefaultFooter();//to change all pages
            for (XWPFParagraph p : ffffff.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if (text != null && text.contains("VPALABRA")) { //if the text contains the word you want to change
                        text = text.replace("VPALABRA", "twitter: GugaMendez"); //change the  word to the new word
                        r.setText(text, 0);
                    }
                }
            }
        }
FileOutputStream out = new FileOutputStream("C:\Users\amendez\Desktop\Agustín\carpetas\write-test4.docx");
                    doc.write(out);
                    out.close();
                    System.out.println("Done perro");
        } catch (Exception ex) {
           ex.printStackTrace();