gpt4 book ai didi

java - Apache poi word文档空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 07:17:33 26 4
gpt4 key购买 nike

我在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)。

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?

最佳答案

我不确定这是否与您写入文档的方式有关,但当您检索文本时,它似乎希望您逐段执行。在下面,我从文档中检索了每个段落,循环检索文本,然后根据需要将其写出来。我已在下面评论了我更改的内容:

public class SO3 {
public static void main(String[] args){

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();

try {
FileOutputStream outStream = new FileOutputStream("D:\\Users\\user2777005\\Desktop\\MyDov.docx");
document.write(outStream);
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}

try{
FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\MyDov.docx"));

XWPFDocument doc = new XWPFDocument(is);
List<XWPFParagraph> paras = doc.getParagraphs(); //This list will hold the paragraphs
XWPFWordExtractor ex = new XWPFWordExtractor(doc); //To get the words
String words = ""; //This will hold all the text
for(XWPFParagraph p : paras){ //For each paragraph we retrieved from the document
words += p.getText(); //Add the text we retrieve to the words string
}

System.out.println(words); //Check out string
XWPFDocument newDoc = new XWPFDocument();
XWPFParagraph para = newDoc.createParagraph();
XWPFRun run = para.createRun();
//You have to reformat the run with bold/italic e.t.c if you want
run.setText(words);
newDoc.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\mydoc.docx")));}
catch (IOException e)
{System.out.println(e);}
}}

祝你好运!

关于java - Apache poi word文档空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19763732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com