Use LightCells API build xls ,throw Exception Object reference not set to an instance of an object

using Aspose.Cells;

namespace ConsoleApp17
{
public class WriteUsingLightCellsAPI
{
public static void Run()
{
// The path to the documents directory.
var fileName = $“F:\temp\{DateTime.Now:yyyyMMHHmmss}.xls”;

        // Specify your desired matrix
        int rowsCount = 10000;
        int colsCount = 30;

        var workbook = new Workbook();
        var saveOptions = new XlsSaveOptions();  // when use XlsSaveOptions PutValue("") throw error
        //var saveOptions = new OoxmlSaveOptions();

        saveOptions.LightCellsDataProvider = new TestDataProvider(workbook, rowsCount, colsCount);

        workbook.Save(fileName, saveOptions);
    }
}

class TestDataProvider : LightCellsDataProvider
{
    private int _row = -1;
    private int _column = -1;

    private int maxRows;
    private int maxColumns;

    private Workbook _workbook;
    public TestDataProvider(Workbook workbook, int maxRows, int maxColumns)
    {
        this._workbook = workbook;
        this.maxRows = maxRows;
        this.maxColumns = maxColumns;
    }

    #region LightCellsDataProvider Members
    public bool IsGatherString()
    {
        return false;
    }

    public int NextCell()
    {
        ++_column;
        if (_column < this.maxColumns)
            return _column;
        else
        {
            _column = -1;
            return -1;
        }
    }
    public int NextRow()
    {
        ++_row;
        if (_row < this.maxRows)
        {
            _column = -1;
            return _row;
        }
        else
            return -1;
    }

    public void StartCell(Cell cell)
    {
        if (_column == 2)
            cell.PutValue("");  // when use XlsSaveOptions PutValue("") throw error
        else
            cell.PutValue(_row + _column);
        if (_row == 1)
        {
        }
        else
        {
            cell.Formula = "=Rand() + A2";
        }
    }

    public void StartRow(Row row)
    {
    }

    public bool StartSheet(int sheetIndex)
    {
        if (sheetIndex == 0)
        {
            return true;
        }
        else
            return false;
    }
    #endregion
}

}

@523218882

The exception “Object reference not set to an instance of an object” typically occurs when you attempt to access a member of an object that has not been initialized. In your case, it seems to happen when you call cell.PutValue("") in the StartCell method of your TestDataProvider class.

To resolve this issue, you should ensure that the cell object is properly initialized before you attempt to use it. Here are a few suggestions to help you troubleshoot and fix the problem:

  1. Check for Null Values: Before calling cell.PutValue(""), you can add a null check to ensure that the cell object is not null. For example:

    public void StartCell(Cell cell)
    {
        if (cell == null)
        {
            throw new ArgumentNullException(nameof(cell), "Cell cannot be null");
        }
    
        if (_column == 2)
            cell.PutValue("");  // Ensure cell is initialized
        else
            cell.PutValue(_row + _column);
    }
    
  2. Review the LightCellsDataProvider Implementation: Make sure that your implementation of the LightCellsDataProvider is correctly set up and that the NextCell and NextRow methods are returning valid indices. If these methods return invalid values, it could lead to uninitialized cells.

  3. Use the Correct Save Options: You are currently using XlsSaveOptions, which is appropriate for the XLS format. However, if you are trying to use features that are more compatible with the XLSX format, consider switching to OoxmlSaveOptions:

    var saveOptions = new OoxmlSaveOptions();
    
  4. Refer to Example Implementations: You can look at example implementations of the LightCells API to ensure that your usage aligns with best practices. For instance, the provided examples in the Aspose documentation demonstrate how to properly implement the LightCellsDataProvider and handle cell data.

Here is a simplified version of your StartCell method that includes a null check:

public void StartCell(Cell cell)
{
    if (cell == null)
    {
        throw new ArgumentNullException(nameof(cell), "Cell cannot be null");
    }

    if (_column == 2)
        cell.PutValue("");  // Ensure cell is initialized
    else
        cell.PutValue(_row + _column);
    
    if (_row != 1)
    {
        cell.Formula = "=Rand() + A2";
    }
}

By implementing these suggestions, you should be able to avoid the “Object reference not set to an instance of an object” exception and successfully use the LightCells API to build your XLS file.

If you continue to experience issues, consider checking the Aspose documentation or community forums for additional insights and examples related to the LightCells API.

Sources:
[1]: UsingLightCellsAPI.cs

WriteUsingLightCellsAPI.zip (937 字节)

使用OoxmlSaveOptions正常,使用XlsSaveOptions会报错

@523218882
We have reproduced the issue and 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-58413

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.

我们已经付费购买了该产品
240822075915
1097343

v25.6.0 能修复该问题吗

@523218882
一般来讲,空引用异常相对容易修复,估计问题不大,一旦修复,我们将第一时间在这里回复您。另外,如果对此需求比较紧急,您也可以参考上面的Paid Support Services .

@523218882

是的,这个问题的fix会包含在下个月发布的25.6中。如果您需要在该版本发布前得到紧急fix,请按前面的链接购买优先级支持服务,我们会尽快修复该问题并提供fix