Search code examples
java

Error in displaying a doc file reading that from a document on console in java


I tried to display the data from a doc file on console then i got this error

run:
The document is really a RTF file
Exception in thread "main" java.lang.NullPointerException
    at DocReader.readDocFile(DocReader.java:36)
    at DocReader.main(DocReader.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

can any one explain where i went wrong

the code is import java.io.File; import java.io.FileInputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class DocReader {
public void readDocFile() {
File docFile = null;
WordExtractor docExtractor = null ;
WordExtractor exprExtractor = null ;
try {
docFile = new File("C:\\web.doc");

FileInputStream fis=new FileInputStream(docFile.getAbsolutePath());


HWPFDocument doc=new HWPFDocument(fis);

docExtractor = new WordExtractor(doc);
}
catch(Exception exep)
{
System.out.println(exep.getMessage());
}


String [] docArray = docExtractor.getParagraphText();

for(int i=0;i<docArray.length;i++)
{
if(docArray[i] != null)
System.out.println("Line "+ i +" : " + docArray[i]);
}
}

public static void main(String[] args) {
DocReader reader = new DocReader();
reader.readDocFile();
}
}

Solution

  • The document is really a RTF file

    That's a typical message of an IllegalArgumentException from the HWPFDocument constructor. To the point it means that the supplied file is actually a (Wordpad) RTF file whose .rtf extension has incorrectly been renamed to .doc.

    Supply a real MS Word .doc file instead and fix your code to not continue the flow when an exception has occurred. You need to throw it.