0

I'm getting a NullReferenceException on a List, and I'm not sure why. Please excuse the spaghetti. I've looked through the line by line debugger, and I'm not seeing any issues. The debugger doesn't throw any exception at all. Does anyone see what's going on here? I've commented where the list is initialized and where the exception is being thrown.

public void ProcessFile()
    {
        var book = new Aspose.Cells.Workbook(File);
        var sheet = book.Worksheets[0];
        var cells = sheet.Cells;

        // List is initialized here 
        List<double> dailyValues = new List<double>(5);

        for (int i = 2; i <= cells.MaxDataColumn; i++)
        {
            Individual associate = new Individual((string) cells[i,0].Value);

            for (int j = 1; j <= cells.MaxDataRow; j++)
            {
                if (j % 6 == 4) continue;

                //Exception being thrown here
                dailyValues.Add((double) cells[i, j].Value);

                if (dailyValues.Count == 5)
                {
                    associate.addDailyNumbers(new DailyNumbers(dailyValues));
                    dailyValues.Clear();
                }
            }
            Associates.Add(associate);
        }
    }
}
Alp
  • 358
  • 5
  • 12
  • 2
    _"through the line by line debugger, and I'm not seeing any issues. The debugger doesn't throw any exception"_ - it sounds like you haven't actually used your debugger then... especially as debuggers don't throw exceptions: the _debuggee_ does, and when it does you'll get a stack-trace (even when no debugger is attached). So, where is your stack-trace and why haven't you posted it? – Dai Aug 03 '22 at 02:18
  • @Dai, I think the OP just means that the when they debug the code the exception isn't thrown. – tymtam Aug 03 '22 at 02:35
  • When you mean something but say something different (less accurate), understanding is hindered. It's important to make that distinction clear *now*. – madreflection Aug 03 '22 at 02:36
  • 1
    I think you're too harsh to a new contributor. – tymtam Aug 03 '22 at 02:37
  • 2
    The exception seem to happen because a value in `cells[i, j]` is null. – tymtam Aug 03 '22 at 02:38
  • Actually, if `cells[i, j]` is null you'd get `'Nullable object must have a value.'` not null reference exception. – tymtam Aug 03 '22 at 02:40
  • Nate, please post the full exception message. – tymtam Aug 03 '22 at 02:41
  • That would be the exception message if `cells[,]` returned a nullable value type (`Nullable`), but it [returns a `Cell` reference](https://reference.aspose.com/cells/net/aspose.cells/cells/item/#cells-indexer-2-of-3), and the `Value` property is on that type, not `Nullable`. – madreflection Aug 03 '22 at 02:46
  • 2
    it looks like you might have maxdatarow and maxdatacolumn flipped. – Muckeypuck Aug 03 '22 at 02:51
  • If you really can't reproduce the problem while debugging, I would really want to spend some time to figure out why, as it could be affecting all of your debugging. It could be something about your runtime environment (e.g. if it normally runs as a service account) or something you are doing to affect the testing (e.g. using a different file). Figure that out first. Then go back and debug the null reference exception by step debugging and inspecting variables. – John Wu Aug 03 '22 at 05:46

0 Answers0