TheAlgorithms/C-Sharp · error

array

Error message

array

What it means

FibonacciSearcher.FindIndex validates its inputs and throws ArgumentNullException with parameter name "array" when the array argument is null. The library requires a non-null array to run the Fibonacci search; null is treated as a programming error, not as 'nothing to search'.

Solutions

  1. Ensure the array is instantiated before calling FindIndex; initialize fields with Array.Empty<T>() instead of leaving them null.
  2. Add a null check or `?? Array.Empty<T>()` coalesce at the call site before invoking the searcher.
  3. Fix the upstream producer so it returns an empty array rather than null on 'no data'.
  4. If null is legitimate, catch ArgumentNullException and handle it explicitly.

Example fix

// before
var idx = searcher.FindIndex(maybeNullArray, item);
// after
var idx = searcher.FindIndex(maybeNullArray ?? Array.Empty<int>(), item);
Defensive patterns

Strategy: validation

Validate before calling

if (array is null) throw new InvalidOperationException("Search array must be initialized before calling FindIndex.");

Type guard

bool HasArray<T>(T[]? array) => array is not null;

Try / catch

try { return searcher.FindIndex(array, item); }
catch (ArgumentNullException ex) when (ex.ParamName == "array") { return -1; }

Prevention

When it happens

Trigger: Calling FindIndex(null, item) — e.g. an array field not yet initialized, a factory method returning null, or a deserialization failure upstream.

Common situations: Arrays produced by LINQ chains that can return null instead of empty; uninitialized class members; API responses mapped to null arrays; test setups that forgot to instantiate the fixture data.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/728a3957be5d18ea. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Search/FibonacciSearcher.cs:24

/// <typeparam name="T">Type of array element.</typeparam>
public class FibonacciSearcher<T> where T : IComparable<T>
{
    /// <summary>
    ///     Finds the index of the item searched for in the array.
    ///     Time complexity:
    ///     worst-case: O(log n),
    ///     average-case: O(log n),
    ///     best-case: O(1).
    /// </summary>
    /// <param name="array">Sorted array to be searched in. Cannot be null.</param>
    /// <param name="item">Item to be searched for. Cannot be null.</param>
    /// <returns>If an item is found, return index. If the array is empty or an item is not found, return -1.</returns>
    /// <exception cref="ArgumentNullException">Gets thrown when the given array or item is null.</exception>
    public int FindIndex(T[] array, T item)
    {
        if (array is null)
        {
            throw new ArgumentNullException("array");
        }

        if (item is null)
        {
            throw new ArgumentNullException("item");
        }

        var arrayLength = array.Length;

        if (arrayLength > 0)
        {
            // find the smallest Fibonacci number that equals or is greater than the array length
            var fibonacciNumberBeyondPrevious = 0;
            var fibonacciNumPrevious = 1;
            var fibonacciNum = fibonacciNumPrevious;

            while (fibonacciNum <= arrayLength)
            {

View on GitHub (pinned to 96e2905cab)