TheAlgorithms/C-Sharp · error

Value cannot be null. (Parameter 'collection')

Error message

Value cannot be null. (Parameter 'collection')

What it means

RecursiveBinarySearcher.FindIndex throws ArgumentNullException with parameter name "collection" (via nameof) when the IList<T> input is null. Binary search needs collection.Count and indexer access, so a null collection is rejected before the recursion starts.

Solutions

  1. Coalesce to an empty list before the call: collection ?? Array.Empty<T>() (with an IList<T> adapter) or return -1 early when null.
  2. Initialize list properties/fields at construction (new List<T>() or Array.Empty<T>()).
  3. Fix upstream APIs to return empty collections rather than null.
  4. Catch ArgumentNullException if a null collection is a meaningful state in your flow.

Example fix

// before
return binarySearcher.FindIndex(items, value);
// after
if (items is null) return -1;
return binarySearcher.FindIndex(items, value);
Defensive patterns

Strategy: validation

Validate before calling

if (collection is null) return -1;

Type guard

bool HasCollection<T>(IList<T>? c) => c is not null;

Try / catch

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

Prevention

When it happens

Trigger: Passing a null IList<T> — a List<T> field never assigned, a method returning null instead of an empty list, or an optional collection parameter left unset.

Common situations: Repository/service layers returning null collections on miss; model binding producing null lists; test fixtures that skip list initialization.

Related errors


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

Appendix: source

Thrown at Algorithms/Search/RecursiveBinarySearcher.cs:23

/// </summary>
/// <typeparam name="T">Type of searcher target.</typeparam>
public class RecursiveBinarySearcher<T> where T : IComparable<T>
{
    /// <summary>
    ///     Finds index of item in collection that equals to item searched for,
    ///     time complexity: O(log(n)),
    ///     space complexity: O(1),
    ///     where n - collection size.
    /// </summary>
    /// <param name="collection">Sorted collection to search in.</param>
    /// <param name="item">Item to search for.</param>
    /// <exception cref="ArgumentNullException">Thrown if input collection is null.</exception>
    /// <returns>Index of item that equals to item searched for or -1 if none found.</returns>
    public int FindIndex(IList<T>? collection, T item)
    {
        if (collection is null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        var leftIndex = 0;
        var rightIndex = collection.Count - 1;

        return FindIndex(collection, item, leftIndex, rightIndex);
    }

    /// <summary>
    ///     Finds index of item in array that equals to item searched for,
    ///     time complexity: O(log(n)),
    ///     space complexity: O(1),
    ///     where n - array size.
    /// </summary>
    /// <param name="collection">Sorted array to search in.</param>
    /// <param name="item">Item to search for.</param>
    /// <param name="leftIndex">Minimum search range.</param>
    /// <param name="rightIndex">Maximum search range.</param>

View on GitHub (pinned to 96e2905cab)