TheAlgorithms/C-Sharp · error
item
Error message
item
What it means
FibonacciSearcher.FindIndex throws ArgumentNullException with parameter name "item" when the search item itself is null. Since searching for null has no defined position in the ordering the searcher assumes, the library rejects it up front as an invalid argument.
Solutions
- Guard the item before calling: if (item is null) return -1; or throw your own descriptive error.
- Fix the source that produced the null key (binding, deserialization, optional field default).
- Use a sentinel/default value of T for reference types when 'no key' should mean 'no search'.
- Catch ArgumentNullException if null keys are expected and should map to not-found.
Example fix
// before return searcher.FindIndex(array, userKey); // after if (userKey is null) return -1; return searcher.FindIndex(array, userKey);
Defensive patterns
Strategy: validation
Validate before calling
if (item is null) return -1; // or surface a validation error to the caller
Type guard
bool HasKey<T>(T? item) => item is not null;
Try / catch
try { return searcher.FindIndex(array, item); }
catch (ArgumentNullException ex) when (ex.ParamName == "item") { return -1; } Prevention
- Default search keys to a non-null sentinel instead of null.
- Validate DTOs/inputs before reaching the search layer.
- In generic wrappers, check default(T) for null on reference types.
- Enable nullable reference type annotations on key-bearing models.
When it happens
Trigger: Calling FindIndex(array, null) — typically a search key that came from an uninitialized variable, a failed map/deserialization, or an optional UI input left empty.
Common situations: Nullable reference types disabled so null keys flow silently; record fields defaulting to null; queries built dynamically where the user-supplied key was never assigned.
Related errors
- array
- searchItem
- sortedArray
- Input string cannot be null.
- Value cannot be null. (Parameter 'collection')
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/98853abd36b285d1.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Search/FibonacciSearcher.cs:29
/// 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)
{
fibonacciNumberBeyondPrevious = fibonacciNumPrevious;
fibonacciNumPrevious = fibonacciNum;
fibonacciNum = fibonacciNumberBeyondPrevious + fibonacciNumPrevious;
}
View on GitHub (pinned to 96e2905cab)