Java Apache poi word文档空指针异常
我在word文档中写了一个字符串。但当我打开文档获取字符串并将其存储在另一个文档中时,它会给我异常 这是我的密码Java Apache poi word文档空指针异常,java,exception,ms-word,apache-poi,Java,Exception,Ms Word,Apache Poi,我在word文档中写了一个字符串。但当我打开文档获取字符串并将其存储在另一个文档中时,它会给我异常 这是我的密码 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraphOne = document.createParagraph(); XWPFRun paragraphOneRunOne = paragraphOne.createRun(); paragraphOneRunOne.setBol
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?
我不确定这是否与您写入文档的方式有关,但当您检索文本时,似乎希望您逐段执行。在下面的例子中,我从文档中检索了每一个段落,在它们之间循环检索文本,然后根据需要将其写出。我在下面评论了我所做的更改:
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);}
}}
公共类SO3{
公共静态void main(字符串[]args){
XWPFDocument document=新的XWPFDocument();
XWPFParagraph paragraphOne=document.createparagraphy();
XWPFRun paragraphOneRunOne=paragraphOne.createRun();
第1.1.段挫折(真);
第1.段斜体字(真);
setText(“你好,世界!这是第一段!”);
段落1.addBreak();
试一试{
FileOutputStream outStream=新的FileOutputStream(“D:\\Users\\user2777005\\Desktop\\MyDov.docx”);
文件。写入(流出);
exptream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
FileInputStream是=新的FileInputStream(新文件(“D:\\Users\\user2777005\\Desktop\\MyDov.docx”);
XWPFDocument doc=新XWPFDocument(is);
List paras=doc.getparations();//此列表将保存段落
XWPFWordExtractor ex=新的XWPFWordExtractor(doc);//获取单词
String words=“;//这将保存所有文本
对于(XWPFParagraph p:paragraph){//对于我们从文档中检索到的每个段落
words+=p.getText();//将检索到的文本添加到words字符串中
}
System.out.println(words);//签出字符串
XWPFDocument newDoc=新XWPFDocument();
XWPFParagraph paragraph=newDoc.createParagraph();
XWPFRun=para.createRun();
//如果需要,您必须使用粗体/斜体e.t.c重新格式化跑步记录
run.setText(单词);
newDoc.write(新文件输出流(新文件(“D:\\Users\\user2777005\\Desktop\\mydoc.docx”);}
捕获(IOE异常)
{System.out.println(e);}
}}
祝你好运 您正在编写文档,然后立即阅读。您是否尝试检查该文档是否正常?您是否尝试使用与MS word兼容的应用程序打开它?你有没有试着阅读其他的,绝对有效的word文档?是的,这个文档还可以,有一句话写着Hello world!这是第一段!我用写字板打开了它。我已经阅读了通过代码创建的其他文档。因此,请附加POI源并尝试使用调试器遍历堆栈。堆栈跟踪不是太深,您将很容易看到抛出NPE的原因。