user3815954
user3815954

Reputation:

Creation of instance of PdfReader throws exception and crash the program

While reading different pdf documents, I have faced with a problem, which is really diffucult to find its reason. I have just run the below source code for two pdf whose properties shown below, and I have got the number of page. However, whenever I have read the third one which is marked with * (PDF 2), my program is crached and I couldnot caught the thrown null exception. My wonder is that why program is crashing and why I can't catch the thrown exception. Moreover, How can I fix this problem?

UPDATE:

itext version: itext 5.5.1 document is purchased, and the claimer is restricts me to share with third person or in the web site

Document properties

 Abbreviation: A: ALLOWED   NA: NOT ALLOWED    NS: NO SECURİTY  AV: ALL VERSION

                                    PDF 1   PDF 2                 PDF 3
                                             *
printing                            | A     A                       A
document assembly                   | NA    NA                      NA
content copying                     | A     A                       A
content copying for accessibility   | A     A                       A
page extraction                     | NA    NA                      NA
commenting                          | A     NA                      A
filling of form fields              | A     NA                      A
signing                             | NA    NA                      NA
creation of template pages          | A     NA                      A
security mode                       | NS    PASSWORD SECURITY       NS
can be opened by                    | AV    ACROBAT 7.0 AND LATER   AV
tagged pdf                          | YES   NO                      NO

                                            ^
                                            ^
                Note of PDF 2: All contents of document are encrypted and search engine cannot access the document's metadata 

The code

    PdfReader pdfReader = null;
    try {
        RandomAccessFile rAF = new RandomAccessFile(this.openFilePath, "r");
        RandomAccessSourceFactory sF = new RandomAccessSourceFactory();
        RandomAccessFileOrArray rA = new RandomAccessFileOrArray( sF.createSource(rAF));

        System.out.println("[DEBUG] - 4");
        System.out.flush();
        pdfReader = new PdfReader(rA, null);

        System.out.println("[DEBUG] - 5");
        System.out.flush();
        this.totalPage = pdfReader.getNumberOfPages();

        System.out.println("[DEBUG] - 6");
        System.out.flush();
    } catch (Exception e) {
        // Technical Exception
        System.out.println("[DEBUG] - 7");
        System.out.flush();
        e.printStackTrace();
    } finally {
        System.out.println("[DEBUG] - In finally clause");
        System.out.flush(); 

line 179        pdfReader.close();

        System.out.println("[DEBUG] - Near Out of finally clause");
        System.out.flush();
    }
    System.out.printf("[DEBUG] - pdfReader\n");
    System.out.flush();

Thrown Exception

[DEBUG] - 4
[DEBUG] - In finally clause
Exception in thread "main" java.lang.NullPointerException
    at extractTotalPageNo(ControlCenter.java:179)
    at control(ControlCenter.java:99)
    at Manage.main(Management.java:22)

Upvotes: 1

Views: 2133

Answers (3)

user8829306
user8829306

Reputation: 1

My 2 cents: In our case it was authorization issue that cause SAP-PI to crash when trying to read the PDF file. This means that this class was not built correctly, if authorization causing the Constructor to crash without issuing Exception.

Upvotes: 0

user3815954
user3815954

Reputation:

I have run the program with @Henry suggestion.

Afterwards, I realized that the system actually results in an error because I forgot to add the BouncyCastle jars to my CLASSPATH. package. When the program tries to read encrypted documents, it calls, I assume, encryption functionality from iText's PdfReader constructor.

Moreover, since iText has a dependency on BouncyCastle, the java compiler complains about not having found the class with the encryption function that is called.

In conclusion, the solution is to add the BouncyCastle jar to my CLASSPATH.

Upvotes: 2

Henry
Henry

Reputation: 43788

From the output it seems that there was a problem in this line

pdfReader = new PdfReader(rA, null);

and pdfreader never got set to something not null.

This leads to the NPE when you try to access it in line 179 and therefore hides the root cause which seems to be a Throwable but not an Exception because it is not caught.

You can make the close conditional:

if (pdfreader != null) {
    pdfReader.close();
}

Upvotes: 1

Related Questions