TheAlgorithms/C-Sharp · error
searchItem
Error message
searchItem
What it means
JumpSearcher.FindIndex throws ArgumentNullException("searchItem") when the value to search for is null. Jump search compares elements with ordering comparisons that are undefined for null, so the null key is rejected during argument validation.
Solutions
- Short-circuit before the call: if (searchItem is null) return -1;
- Ensure default(T) is not leaked into the search path in generic wrappers.
- Populate or default the key at the source (form binding, DTO default) so it is never null.
- Catch ArgumentNullException and map it to a not-found result if null keys are an expected input state.
Example fix
// before return jumper.FindIndex(sortedArray, key); // after if (key is null) return -1; return jumper.FindIndex(sortedArray, key);
Defensive patterns
Strategy: validation
Validate before calling
if (searchItem is null) return -1;
Type guard
bool IsValidKey<T>(T? key) => key is not null;
Try / catch
try { return jumper.FindIndex(sortedArray, searchItem); }
catch (ArgumentNullException ex) when (ex.ParamName == "searchItem") { return -1; } Prevention
- Never pass default(T) of a reference type straight into search APIs.
- Bind optional UI inputs to non-nullable defaults or empty sentinels.
- Validate the key in your own service layer before delegating to the searcher.
- Add assertions in tests for null-key behavior.
When it happens
Trigger: FindIndex(sortedArray, null) — null coming from optional parameters, uninitialized key variables, or nullable data models where the search field was never populated.
Common situations: Search boxes/inputs bound to nullable properties; records where an optional key is absent; generic code where default(T) is null for reference types and is passed through unchecked.
Related errors
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/d07c3dd849885dc8.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Search/JumpSearcher.cs:27
/// <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;
if (nextIndex >= sortedArray.Length)
{
nextIndex = sortedArray.Length - 1;
break;
}View on GitHub (pinned to 96e2905cab)