2013-11-04 120 views
1

我已经在word文档中写了一个字符串。但是,当我打开文档来获取字符串并将其存储在另一个文档中时,它给了我例外。Apache poi 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? 
+0

您正在编写您的文档,然后立即阅读它。您是否尝试检查该文件是否正常?您是否尝试用MS word兼容的应用程序打开它?您是否尝试阅读* other *,绝对有效的单词文档? – AlexR

+0

是这个文件还行有一句写着Hello world的句子!这是第一段!我用写字板打开了它。我已阅读通过代码创建我的其他文档。 – Sarah

+0

因此,请附上POI的来源,并尝试通过调试器浏览堆栈。堆栈跟踪不太深,你会很容易看到为什么NPE被抛出。 – AlexR

回答

1

我不确定它是否与你正在写的文件,但是当你检索文本似乎希望你通过一段做一段的方式做。在下面,我已经从文档中检索了每个段落,通过它们循环检索文本,然后按照需要写出它。我已经评论了我改变如下:

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

祝你好运!

+0

谢谢你的工作:) – Sarah