我正在尝试编写一个 Java 程序来计算 PDF 文件的页数。但是当我运行这个程序时,我得到一个错误,我不知道为什么。
这是错误:
Exception in thread "main" java.lang.NullPointerException at pdfpagecount.Pdfpagecount.main(Pdfpagecount.java:12)
这是产生错误的代码:
package pdfpagecount;
import java.io.File;
import java.io.FileInputStream;
import com.lowagie.text.pdf.PdfReader;
public class Pdfpagecount {
public static void main(String[] args) {
File gopi = new File("C:\\Users\\Gopinath Muruti\\Desktop\\test.pdf");
File listOfFile[] = gopi.listFiles();
for(int i = 0; i < listOfFile.length; i++) {
File tempFile = listOfFile[i];
String fileName = tempFile.getName();
System.out.println("File Name = " + fileName);
try {
if(fileName.toLowerCase().indexOf(".pdf") != -1) {
PdfReader document = new PdfReader(new FileInputStream(new File("filename")));
int noPages = document.getNumberOfPages();
System.out.println("Number of Pages in the PDF document is = " + noPages);
}
}
catch(Exception e) {
System.out.println("Exception : " + e.getMessage());
e.printStackTrace();
}
}
}
}
ABOUTYOU
RISEBY
相关分类