Question: I'm trying to select a specific sheet (by name or index) with my excel Add-In with no avail. 

My addin file `ThisAddIn.cs` has:

```
public Excel.Workbook GetActiveWorkbook()
{
    return (Excel.Workbook)Application.ActiveWorkbook;
}

```

And my `Ribbon1.cs` has:

```
namespace Test3
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
            Debug.WriteLine("Hello");
            Workbook currentwb = Globals.ThisAddIn.GetActiveWorkbook();
            Worksheet scratch = currentwb.Worksheets.Item[1] as Worksheet; // Error blocks here
            if (scratch == null)
                return;
            // Worksheet scratch = currentwb.Worksheets["Sheets1"];
            scratch.Range["A1"].Value = "Hello";
        }
    }
}

```

But I get a `System.NullReferenceException: 'Object reference not set to an instance of an object.'`

I'm new to c# (come from Python) and am very confused why this doesn't work. Any help would be greatly appreciated.

Answer: 
It all has to do with what is in the accumulator (`high`). If you don't provide the second argument to `reduce`, the accumulator starts off as the first object, and the current element is the second element. In your first iteration, you treat the accumulator as the object, fetching the grade using `high.grade`; but then you return a number (`94`), and not an object, to be your next accumulator. In the next iteration of the loop, `high` is not an object any more but `94`, and `(94).grade` makes no sense.

When you remove the third element, there is no second iteration, and there is no time for the bug to occur, and you get the value of the current accumulator (`94`). If there were only one element, you'd get the initial accumulator (`{name: 'Leah', grade: 94}`). This is, obviously, not ideal, as you can't reliably predict the shape of the result of your calculation (object, number or error).

You need to decide whether you want the number or the object, one or the other.

```
let highest = students.reduce(
    (high, current) => Math.max(high, current.grade),
    Number.NEGATIVE_INFINITY
)

```

This variant keeps the accumulator as a number, and will return `94`. We can't rely on the default starting accumulator, as it needs to be a number, so we set it artificially at `-INF`.

```
let highest = students.reduce(
    (high, current) => high.grade > current.grade ? high : current,
)

```

This is the object version, where `highest` ends up with `{name: 'Leah', grade: 94}`.
The issue here is the accumulator (high) after the first pass is a number (what gets returned by math.max), yet each pass requires that high be an object with grade as a number property. So by the second call you're calling `Math.max(undefined, 73)` - which will return `NaN`. Instead I'd recommend initializing the accumulator with `-Infinity` and only supplying `high`:

`let highest = students.reduce(
 (high, current) => Math.max(high, current.grade)
 , -Infinity)`