I am trying to code a Java program to calculate the number of pages for PDF files. But when I run this program I get an error which I am not sure why.
This is the error:
Exception in thread "main" java.lang.NullPointerException at pdfpagecount.Pdfpagecount.main(Pdfpagecount.java:12)
Here is the code that produces the error:
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();
}
}
}
}