Java:XWPFWordExtractor.getText()抛出NullPointerException

Java:XWPFWordExtractor.getText()抛出NullPointerException,java,nullpointerexception,ms-word,apache-poi,docx,Java,Nullpointerexception,Ms Word,Apache Poi,Docx,我正在使用Apache POI创建一个带有以下代码的.docx文件: XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(text); String filePath = outputPathWithoutExtension + ".docx"; try {

我正在使用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());
}
然后我试着用下面的代码来阅读它:

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
。。。在我的例子中是空的。当它试图在第一行使用它时。。。轰

我尝试创建自己的“页眉/页脚策略”,如下所示:

但是,这本身会引发一个
NullPointerException
,因为它试图通过
doc.getDocument().getBody().getSectPr()
访问文档的“SectPr”,它返回null。。。然后它第一次使用这个。。。轰

所以,我的问题是:我显然没有正确地创建
XWPFDocument
。。。有人能帮我澄清一下吗


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

Aha!我在这里找到了答案:

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

// 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");
}
/--------------修改页脚或页眉更改页脚x页眉
//如果你需要帮助,告诉我。agustinoscarmendez@gmail.com
试一试{
XWPFDocument doc=新的XWPFDocument(OPCPackage.open(“C:\\Users\\amendez\\Documents\\NetBeansProjects\\puertasrl\u prespuesto\\Resources\\Words y Excel Examples\\Modelos de EPC.docx”);
//XWPFDocument doc=新XWPFDocument();
CTSectPr sectPr=doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy=新的XWPFHeaderFooterPolicy(doc,sectPr);
XWPFHeaderFooterPolicy hfp=doc.getHeaderFooterPolicy();
//XWPFFooter ffffff=hfp.getFooter(1);//帕拉·坎比亚尔·佩特尔·德拉普里梅拉·帕吉纳
xwpfooter ffffff=hfp.getDefaultFooter();//更改所有页面
对于(XWPFParagraph p:ffffff.getparagraph()){
List runs=p.getRuns();
如果(运行!=null){
用于(XWPFRun r:运行){
String text=r.getText(0);
if(text!=null&&text.contains(“VPALABRA”){//如果文本包含要更改的单词
text=text.replace(“VPALABRA”,“twitter:GugaMendez”);//将单词更改为新词
r、 setText(text,0);
}
}
}
}
FileOutputStream out=新的FileOutputStream(“C:\\Users\\amendez\\Desktop\\Agustín\\地毯\\ write-test4.docx”);
写(出)文件;
out.close();
System.out.println(“完成perro”);
}捕获(例外情况除外){
例如printStackTrace();

看起来是一个bug-您是否尝试将其作为bug报告给?