Object reference exception in the ReplacingCallback of FindReplaceOptions after upgrading to 25.1

When calling InsertHtml from within the ReplacingCallback of an FindReplaceOptions instance that is passed into a Range.Replace, we are getting an object reference exception. This just started happening after upgrading to Aspose.Words 25.1. Our code did not change, but we were on a year or two old version before we upgraded.

This error happens even if the HTML is simple text without any HTML tags.

The stack trace is not very helpful:

@jsierks

It seems that you are encountering an Object reference exception when using the InsertHtml method within the ReplacingCallback of a FindReplaceOptions instance after upgrading to Aspose.Words 25.1. This issue may arise due to changes in the library’s internal handling of document elements or the context in which the callback is executed.

Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check for Null References: Ensure that all objects you are accessing within the ReplacingCallback are properly initialized. The exception may occur if you are trying to access a property or method on a null object.

  2. Simplify the HTML: Since you mentioned that the error occurs even with simple text, try to isolate the problem by using the most basic HTML possible. This can help determine if the issue is related to specific HTML content.

  3. Review the Callback Implementation: Make sure that your implementation of the IReplacingCallback interface is correctly handling the replacement logic. Pay special attention to how you are using the ReplacingArgs parameter.

  4. Test with Previous Version: If possible, revert to the previous version of Aspose.Words to confirm that the issue is indeed related to the upgrade. This can help isolate whether the problem is with your code or a change in the library.

  5. Consult Documentation and Release Notes: Review the release notes for version 25.1 to see if there are any breaking changes or updates related to the FindReplaceOptions or ReplacingCallback that might affect your implementation.

  6. Seek Community Help: If the issue persists, consider posting a detailed question on the Aspose forums, including your code snippet and the exact error message. The community or Aspose support may provide insights based on similar experiences.

If you can provide more details about your implementation or the specific code that triggers the exception, I may be able to offer more targeted advice.

  1. It’s your code with the null. I don’t know what it is.
  2. Still exceptions.
  3. The same code works for some Word docs, but not all of them.
  4. I am going to try this next…
  1. I did. Nothing about replace in 25.1 Aspose.Words for .NET 25.1 Release Notes
  2. Here I am.

@jsierks Could you please create a simple console application that will allow us to reproduce the problem? We will check the scenario on our side and provide you more information.

Version 24.2 is the last version that works.

@jsierks Could you please create a simple console application that will allow us to reproduce the problem and attach it here? We will check the scenario on our side and provide you more information. I tested with simple code and everything works fine on my side. So probably there is something with your IReplacingCallback implementation or your input document.