我在word文档中写了一个字符串。但是当我打开文档以获取字符串并将其存储在另一个文档中时,它给了我异常。
这是我的代码。
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraphOne = document.createParagraph();
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Hello world! This is paragraph one!");
paragraphOneRunOne.addBreak();
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream("MyDov.docx");
document.write(outStream);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try{
FileInputStream is = new FileInputStream(new File("MyDov.docx"));
XWPFDocument doc = new XWPFDocument(is); //Document with words
XWPFWordExtractor ex = new XWPFWordExtractor(doc); //To get the words
String words = ex.getText();
XWPFDocument newDoc = new XWPFDocument(); //Doc to write new doc to
XWPFParagraph para = newDoc.createParagraph(); //Paragraph
XWPFRun run = para.createRun();
run.setText(words);
}
newDoc.write(new FileOutputStream(new File("mydoc.docx")));}
catch (IOException e)
{System.out.println(e);}
它给了我这个例外。
Exception in thread "main" 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)
at atestpack.CreateDocumentFromScratch.main(CreateDocumentFromScratch.java:56)
How can I solve this exception?