PDF Merge returns Object reference not set to an instance of an object System.NullReferenceException

Using the PdfFileEditor.Concatenate method is throwing a “Object reference not set to an instance of an object System.NullReferenceException” error. Both files intended to merge exist and confirm they exist. We can merge them using other applications (eg: Adobe Acrobat) and the files merge without an issue, so it does not appear to be the files causing the problem. I also tried to flatten the files (in code) and this still results in an error. To me, it appears as if the ‘outputFile’ is not being properly created and hence throws the error.

Any help on this would be greatly appreciated. Thank you.

@BryanWenc

Cause

The “Object reference not set to an instance of an object” error typically indicates that there is a null reference in your code. This can occur if the outputFile is not being properly initialized or if one of the input files is not being handled correctly during the concatenation process.

Solution

To resolve this issue, you can take the following steps:

  1. Check File Paths: Ensure that the paths to the input PDF files are correct and that the files exist at those locations.
  2. Initialize Output File: Make sure that the outputFile variable is properly initialized before you call the Concatenate method.
  3. Use Try-Catch: Implement error handling to catch any exceptions and log them for further analysis.

Code

Here’s a sample code snippet that demonstrates how to concatenate PDF files while ensuring proper error handling:

using Aspose.Pdf.Facades;

public void MergePdfFiles(string inputFile1, string inputFile2, string outputFile)
{
    try
    {
        // Create PdfFileEditor object
        PdfFileEditor pdfEditor = new PdfFileEditor();

        // Check if input files exist
        if (!File.Exists(inputFile1) || !File.Exists(inputFile2))
        {
            throw new FileNotFoundException("One or more input files do not exist.");
        }

        // Concatenate files
        pdfEditor.Concatenate(inputFile1, inputFile2, outputFile);
        Console.WriteLine("PDF files merged successfully.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"An error occurred: {ex.Message}");
    }
}

Next Steps

  1. Run the code and check if the error persists.
  2. If the issue continues, consider checking the contents of the PDF files for any corruption or unsupported features.
  3. Enable logging to capture more details about the error.

Please implement these steps and let me know if you need further assistance!

The files do exist and confirm as existing. All of our objects are properly defined, fully exist, and confirmed. The error is not within our code but in the code of the Concatenate() method. This again pointes to the outputFile parameter as the potential issue.

We will need further assistance.

@BryanWenc
Please attach documents and the code fragment you used so that we can reproduce and study the problem.

hello, here is a snippet of the code. I cannot currently include the files as they contain sensitive information and we are confirming that we can post the information here. I will update when I can.

public override void MergePDFs(string[] locationMergeFiles) {

	var pdfEditor = new PdfFileEditor();
	var tempFile = "C:\TempFile";
	
	try {
		if (!pdfEditor.Concatenate(locationMergeFiles, tempFile)) {
			log.Error("Failed to merge PDFs");
			throw new PdfException($"Failed to merge PDFs");
		}
	}
	finally {
		// File cleanup
	}
}

@BryanWenc
Unfortunately, without the original documents, we are unlikely to be able to reproduce and understand the problem. If you remove sensitive information from the documents with which the error occurs, will the error be reproduced?
Please attach a stack trace of the exception - perhaps this will provide enough information.

Hello,

I apologize for delay in my response but we had to run the issue with our legal team as we would need to send you confidential documents. We have cleared that hurdle and I now have a pair of files that will fail to merge, using the code included above.

So, as mentioned, attached are a pair of files that give us the error message. Please let me know what we can do to help expedite this from here forward.

Thank you.
PCL (borr)- CONFIDENTIAL.pdf (78.5 KB)

Supporting Docs- CONFIDENTIAL.pdf (3.6 MB)

@BryanWenc
Thank you, I will check and write to you soon.

@BryanWenc
Thanks for the information provided.
The issue was reproducible, and I’ve created a task for the developers.

@BryanWenc
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-60892

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.

I am sorry but the link you included for Free Support Policies brings up a “Page now found” error and displays “Oops! That page is private.” when I click it. How am I to find out what the status is for this issue if that link is inaccessible?

Thank you

@BryanWenc
Are you referring to the underlined link at the top?
I’ll pass this information on to the appropriate specialists.
I’d also like to clarify: can you see the task status (the underlined link at the bottom)?
1.png (41.0 KB)

Yes, the upper link goes to the “bad” page. Thank you for passing it on.

And yes, I do see the details below- I noticed it like the day after I posted my response but thank you for the clarification!

@BryanWenc

This has been fixed and now opens on my computer.

Just need to check in here - any update on this other than “open”?

@BryanWenc
Nothing new at the moment.