NullReferenceException in Document.OptimizeResources

See the attached demo project, which throws the following exception:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at #=zRfa24k6qRV51vWOnVLP8VICdg7c7CNQNesYvZJY=.#=zIurEAP4ADiq5(Boolean #=zIk6htSI4qTQV_wsAtjgkVrw=)
   at #=zRfa24k6qRV51vWOnVLP8VICdg7c7CNQNesYvZJY=.#=zrvDNyKc=()
   at #=zFt484_l9KEYCyAvRzjPDLLmGNHYc.#=zgvVm2FA=(OptimizationOptions #=z7nIVpLE=)
   at Aspose.Pdf.Document.OptimizeResources(OptimizationOptions strategy)
   at Program.<Main>$(String[] args) in C:\Users\ken\source\repos\AsposeNullRefDemo\Program.cs:line 25

We see this error on a variety of valid uploaded PDF documents. I’ve included one in the sample solution, but more are available on request if necessary.

AsposeNullRefDemo.zip (1.4 MB)

@smithkl42

The NullReferenceException you are encountering in the Document.OptimizeResources method typically indicates that there is an attempt to access an object that has not been initialized. This can happen for various reasons, such as missing or improperly configured optimization options.

To troubleshoot this issue, consider the following steps:

  1. Check Initialization: Ensure that all objects, especially the OptimizationOptions, are properly initialized before they are used. For example, make sure you are creating an instance of OptimizationOptions correctly.

  2. Review the Input Document: Verify that the PDF document you are trying to optimize is not corrupted and is accessible. If the document is null or not properly loaded, it could lead to a NullReferenceException.

  3. Example Code: Here is a basic example of how to optimize a PDF document using Aspose.PDF, which you can compare with your implementation:

    import com.aspose.pdf.Document;
    import com.aspose.pdf.optimization.OptimizationOptions;
    
    public class OptimizePDFExample {
        public static void main(String[] args) {
            // Load the PDF document
            Document pdfDocument = new Document("path/to/your/document.pdf");
    
            // Create optimization options
            OptimizationOptions optimizationOptions = new OptimizationOptions();
            optimizationOptions.setRemoveUnusedObjects(true);
            optimizationOptions.setSubsetFonts(true);
    
            // Optimize the document
            pdfDocument.optimizeResources(optimizationOptions);
    
            // Save the optimized document
            pdfDocument.save("path/to/your/optimized_document.pdf");
        }
    }
    
  4. Debugging: If the issue persists, consider adding logging or debugging statements to identify which object is null at the time of the exception. This can help pinpoint the exact cause of the problem.

If you continue to experience issues, please provide more details about your implementation or any specific configurations you are using, and I would be happy to assist further.

Sources:
[1]: OptimizePDFFileSize.java
[2]: OptimizePDFDocumentForWeb.java

Appreciate the AI response, but the issue isn’t in our code, it’s somewhere down deep in the obfuscated bowels of the Aspose PDF library.

I should add that the problem appears to be in this line here:

    CompressObjects = true,

If I change that to:

    CompressObjects = false,

Then the compression bit works on that file without the NRE.

@smithkl42

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): PDFNET-59893

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.

1 Like