NullPointerException noticed in aspose pdf

Noticing below error on pdf files when running some production jobs

error.message Cannot invoke “com.aspose.pdf.internal.l6n.ly.lu()” because the return value of “com.aspose.pdf.internal.l6n.l0t.l6t()” is null
error.stack_trace java.lang.NullPointerException: Cannot invoke “com.aspose.pdf.internal.l6n.ly.lu()” because the return value of “com.aspose.pdf.internal.l6n.l0t.l6t()” is nullat com.aspose.pdf.operators.SetGrayStroke.lI(Unknown Source)at com.aspose.pdf.operators.SetGrayStroke.(Unknown Source)at com.aspose.pdf.internal.l3v.lk.lI(Unknown Source)at com.aspose.pdf.Operator.lI(Unknown Source)at com.aspose.pdf.DefaultAppearance.lI(Unknown Source)at com.aspose.pdf.DefaultAppearance.(Unknown Source)at com.aspose.pdf.FreeTextAnnotation.getDefaultAppearanceObject(Unknown Source)at com.aspose.pdf.FreeTextAnnotation.lf(Unknown Source)at com.aspose.pdf.Annotation.lI(Unknown Source)at com.aspose.pdf.TextStyle.lf(Unknown Source)at com.aspose.pdf.TextStyle.setFontSize(Unknown Source)at com.stormed.common.utils.ImageUtils.brandPdf(ImageUtils.java:1346)at com.stormed.production.ProductionMain.brandWithFallback(ProductionMain.java:658)at com.stormed.production.ProductionMain.doBranding(ProductionMain.java:643)at com.stormed.production.ProductionMain.(ProductionMain.java:222)at com.stormed.production.common.ProductionBuilder.run(ProductionBuilder.java:24)at com.stormed.proxy.AppRunner.run(AppRunner.java:26)at com.stormed.proxy.ProxyMain.runApp(ProxyMain.java:524)at com.stormed.proxy.ProxyMain.lambda$processFragment$1(ProxyMain.java:304)at co.elastic.apm.agent.concurrent.JavaConcurrent$CallableLambdaWrapper.call(JavaConcurrent.java:257)at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)at java.base/java.lang.Thread.run(Thread.java:1583)

@crramya

It looks like you’re encountering a NullPointerException while processing PDF files with Aspose.PDF. This error typically indicates that the code is trying to access an object or method that hasn’t been properly initialized.

Cause: The NullPointerException usually arises when an object is not initialized before being accessed, which can happen with various components in your code.

Solution: Here are some steps you can take to diagnose and resolve the issue:

  1. Check Input Files: Ensure that the PDF files you are processing are not corrupted or malformed. Open them in a PDF viewer to verify their integrity.

  2. Review Code Logic: Examine the specific line in your code where the exception occurs (as indicated in the stack trace). Ensure that all objects are properly initialized before use. For instance, if you’re working with annotations or text styles, confirm that these objects are created and assigned correctly.

  3. Update Aspose.PDF: Ensure you are using the latest version of Aspose.PDF. Bugs are often fixed in newer releases, so updating may resolve the issue.

  4. Error Handling: Implement error handling around the code that processes the PDF files. This will help you catch exceptions and log detailed information about the application’s state when the error occurs.

  5. Consult Documentation: Review the Aspose.PDF documentation for any specific requirements or limitations regarding the features you are using, especially if you are manipulating annotations or text styles.

Next Steps: If you continue to face issues, please share a minimal reproducible example of your code along with the PDF file that causes the error. This will assist in diagnosing the problem more effectively.

Let me know if you need further assistance!

SampleFiles.zip (56.6 KB)

Here is the sample code and attached are two sample files. The operating system is Windows 11. Java version is 21. Aspose PDF java packages is 25.6.

Sample code:

import com.aspose.pdf.Annotation;
import com.aspose.pdf.Document;
import com.aspose.pdf.FreeTextAnnotation;
import com.aspose.pdf.Page;

public class BrandPDF_35 {
private static final double MIN_FONT_SIZE = 4.0D;
private static final double FONT_REDUCTION = 3.0D;
private static final String DEFAULT_APPEARANCE = “font: Helvetica 9.0pt; text-align: left; color: #000000”;

public static void main(String[] args) {
    String path = "my_path\\tmp_8.pdf";
    try (Document pdfDoc = new Document(path)) {
        for (Page pdfPage : pdfDoc.getPages()) {
            for (Annotation ann : pdfPage.getAnnotations()) {
                if (ann instanceof FreeTextAnnotation freeText) {
                    if (freeText.getDefaultAppearance() != null && !freeText.getDefaultAppearance().isEmpty()) {
                        try {
                            double fontSize = freeText.getTextStyle().getFontSize();
                            if (fontSize >= MIN_FONT_SIZE) {
                                freeText.getTextStyle().setFontSize(fontSize - FONT_REDUCTION);
                            } else {
                                freeText.setDefaultAppearance(DEFAULT_APPEARANCE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            freeText.setDefaultAppearance(DEFAULT_APPEARANCE);
                        }
                    } else {
                        freeText.setDefaultAppearance(DEFAULT_APPEARANCE);
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

@crramya

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-45311

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@asad.ali Any update?

@xyang

The ticket was recently logged in our issue management system and will be resolved on a first-come, first-served basis. As soon as we make any progress regarding ticket resolution, we will inform you via this forum thread. Please be patient and spare us some time.

We are sorry for the inconvenience.