PDFTextStripper NullPointerException [英] PDFTextStripper NullPointerException
问题描述
我正在尝试使用 apache PDFBox(1.8.9) 从 Java 中的 PDF 文件中获取一些数据.我已经在我的构建路径和类路径中添加了 jar(在 Eclipse-Mars 中)
I am trying to fetch some data from a PDF file in Java using apache PDFBox(1.8.9). I have added the jar in my buildpath and classpath (in Eclipse-Mars)
我在创建 PDFTextStripper
对象时遇到空指针异常.
I am getting a null pointer exception while creating a PDFTextStripper
object.
import java.io.File;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.pdmodel.PDDocument;
public class MainClass {
public static void main(String[] args) {
PDDocument pd ;
try{
StringBuilder sb = new StringBuilder();
File input = new File("C:\\Result.pdf");
pd = PDDocument.load(input);
PDFTextStripper s = new PDFTextStripper();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我得到的错误是:
java.lang.NullPointerException
at org.apache.pdfbox.util.TextNormalize.findICU4J(TextNormalize.java:54)
at org.apache.pdfbox.util.TextNormalize.<init>(TextNormalize.java:45)
at org.apache.pdfbox.util.PDFTextStripper.<init>(PDFTextStripper.java:229)
at MainClass.main(MainClass.java:17)
(第 17 行是我尝试创建 PDFTextStripper 对象的地方)
(Line 17 is where I am trying to create a PDFTextStripper object)
推荐答案
您缺少一些依赖项,请确保您的类路径中存在以下三个 jar:-
You are missing some dependency, please ensure below three jars are present in your classpath:-
我用上述三个 jar 执行了你问题中提到的代码,没有收到任何 NPE.
I executed the code mentioned in your question with the above three jars, didn't receive any NPE.
还请检查您的 pdfbox-1.8.9.jar,确保它没有损坏.
TextStripper 类存在于 pdfbox-1.8.9.jar 中,所以在我看来这个 jar 已损坏.
再次下载 jar 并尝试.
Also kindly check your pdfbox-1.8.9.jar, ensure that its not corrupted.
TextStripper class is present in pdfbox-1.8.9.jar, so It looks to me that this jar is corrupted.
Download the jar again and try.
这篇关于PDFTextStripper NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!