TheAlgorithms/C-Sharp · error

sortedArray

Error message

sortedArray

What it means

JumpSearcher.FindIndex requires a non-null sorted array and throws ArgumentNullException("sortedArray") when it is null. Jump search computes step sizes from array.Length, so a null array is rejected immediately as a caller contract violation.

Solutions

  1. Coalesce at the call site: sortedArray ?? Array.Empty<T>() (FindIndex then legitimately returns -1 for empty input).
  2. Initialize the array in the constructor/field initializer so it is never null.
  3. Make the upstream loader return an empty array on failure instead of null.
  4. Catch ArgumentNullException only when null is a recoverable, expected state.

Example fix

// before
var idx = jumper.FindIndex(loadedArray, key);
// after
var idx = jumper.FindIndex(loadedArray ?? Array.Empty<int>(), key);
Defensive patterns

Strategy: validation

Validate before calling

if (sortedArray is null) return -1;

Type guard

bool HasData<T>(T[]? arr) => arr is not null;

Try / catch

try { return jumper.FindIndex(sortedArray, searchItem); }
catch (ArgumentNullException ex) when (ex.ParamName == "sortedArray") { return -1; }

Prevention

When it happens

Trigger: Passing a null array reference to FindIndex — uninitialized fields, methods that return null instead of empty arrays, or data not yet loaded from I/O.

Common situations: Cache misses returning null; JSON/API deserialization leaving arrays null; forgetting to sort-and-assign before the lookup step in a pipeline.

Related errors


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

Appendix: source

Thrown at Algorithms/Search/JumpSearcher.cs:22

///     Jump Search checks fewer elements by jumping ahead by fixed steps.
///     The optimal steps to jump is √n, where n refers to the number of elements in the array.
///     Time Complexity: O(√n)
///     Note: The array has to be sorted beforehand.
/// </summary>
/// <typeparam name="T">Type of the array element.</typeparam>
public class JumpSearcher<T> where T : IComparable<T>
{
    /// <summary>
    ///     Find the index of the item searched for in the array.
    /// </summary>
    /// <param name="sortedArray">Sorted array to be search in. Cannot be null.</param>
    /// <param name="searchItem">Item to be search for. Cannot be null.</param>
    /// <returns>If item is found, return index. If array is empty or item not found, return -1.</returns>
    public int FindIndex(T[] sortedArray, T searchItem)
    {
        if (sortedArray is null)
        {
            throw new ArgumentNullException("sortedArray");
        }

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

        int jumpStep = (int)Math.Floor(Math.Sqrt(sortedArray.Length));
        int currentIndex = 0;
        int nextIndex = jumpStep;

        if (sortedArray.Length != 0)
        {
            while (sortedArray[nextIndex - 1].CompareTo(searchItem) < 0)
            {
                currentIndex = nextIndex;
                nextIndex += jumpStep;

View on GitHub (pinned to 96e2905cab)