CalculateFormula throws an exception

I am using
Aspose.Cells 25.10.0
.NET 9.0

There is a file that passes CalculateFormula in version 25.9 but in version 25.10 CalculateFormula throws an exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at .( )
   at Aspose.Cells.Workbook.(Boolean , CalculationOptions , e )
   at Aspose.Cells.Workbook.CalculateFormula(CalculationOptions options)
   at Program.<Main>$(String[] args) in ...

Code:

var input = @"C:\input_for_aspose.xlsx";
var output = @"C:\output_for_aspose.xlsx";

Workbook? wbc;
using (var ms = new FileStream(input, FileMode.Open))
    wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));

try
{
    var engine = new CustomCalculationEngine()
    {
        
    };

    var opts = new CalculationOptions
    {
        CustomEngine = engine
    };
    
    wbc.CalculateFormula(opts);

    wbc.Save(output);
}
catch (Exception e)
{
    Console.WriteLine(e);
    throw;
}

public class CustomCalculationEngine : AbstractCalculationEngine
{
    public override void Calculate(CalculationData data)
    {
        try
        {
            var val = data.Cell.Value;
            data.Cell.Formula = data.Cell.R1C1Formula = null;

            data.Cell.Value = val;
            data.CalculatedValue = data.Cell.Value;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}

input file: input_for_aspose.zip (1014.1 KB)

@aleksandr.l

Summary
Workbook.CalculateFormula works in Aspose.Cells 25.9 but throws a NullReferenceException in 25.10 when a custom calculation engine is used. The exception is caused by the engine trying to access data.Cell for cells that are null (typically error cells such as #REF!). In 25.10 the library performs stricter null‑checking, so the missing guard now surfaces as an exception.

Why it happens

  1. The workbook contains cells whose evaluation results in errors (#REF!, #NAME?, etc.).
  2. During calculation Aspose.Cells creates a CalculationData instance for each cell that needs processing. For error cells the Cell property can be null.
  3. Your CustomCalculationEngine.Calculate method accesses data.Cell (e.g., data.Cell.Value, data.Cell.Formula) without first verifying that the property is non‑null. When data.Cell is null, a NullReferenceException is thrown.
  4. In version 25.9 the library silently ignored the null reference; version 25.10 now propagates it, exposing the issue in custom engines.

What to do next

  1. Add null‑checks (and optional error checks) in the custom engine
using Aspose.Cells;

public class CustomCalculationEngine : AbstractCalculationEngine
{
public override void Calculate(CalculationData data)
{
// Skip processing if the cell reference is missing
if (data?.Cell == null)
return;

// Optionally ignore cells that already contain an error value
if (data.Cell.IsError)
return;

try
{
var originalValue = data.Cell.Value;

// Remove the formula so the cell becomes a plain value
data.Cell.Formula = null;
data.Cell.R1C1Formula = null;

// Restore the original value
data.Cell.Value = originalValue;
data.CalculatedValue = originalValue;
}
catch (Exception ex)
{
// Log the exception as needed; re‑throw only if you want the caller to see it
Console.WriteLine(ex);
throw;
}
}
}
  1. Pass the custom engine (and, if desired, the error‑ignore flag) via CalculationOptions
var options = new CalculationOptions
{
CustomEngine = new CustomCalculationEngine(),
// The following flag tells the engine to skip cells that evaluate to an error.
// Set it only if you do not need to process those cells.
IgnoreError = true
};

workbook.CalculateFormula(options);
  1. Verify the change
  • Re‑run the calculation with the updated engine.
  • The workbook should now complete without raising a NullReferenceException, even when error cells are present.
  1. If the exception still occurs

  2. Confirm that you are using the latest available Aspose.Cells build (a newer patch may contain a regression fix).

  3. If the problem persists, create an internal support ticket, attach the workbook and the full stack trace, and note that the crash happens only with a custom AbstractCalculationEngine on version 25.10. This will allow the product team to investigate a possible regression.

Following these steps should eliminate the NullReferenceException in version 25.10 and restore normal calculation behavior.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

@aleksandr.l,

Thanks for the template XLSX file and details.

I reproduced the issue as you mentioned by using your sample Excel file and code snippet. I found an exception: “System.NullReferenceException: Object reference not set to an instance of an object” on Workbook.CalculateFormula() method.

We require thorough evaluation of the issue. 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): CELLSNET-59304

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.