TheAlgorithms/Java · error · IllegalArgumentException

Input array must not be empty.

Error message

Input array must not be empty.

What it means

Thrown by FibonacciSearch.find(T[], T) when the input array has length 0. Fibonacci search requires at least one element to establish its Fibonacci-based partitioning. The guard runs before the sortedness check, so empty arrays fail first.

Source

Thrown at src/main/java/com/thealgorithms/searches/FibonacciSearch.java:33

 * Note: This algorithm requires that the input array be sorted.
 * </p>
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class FibonacciSearch implements SearchAlgorithm {

    /**
     * Finds the index of the specified key in a sorted array using Fibonacci search.
     *
     * @param array The sorted array to search.
     * @param key The element to search for.
     * @param <T> The type of the elements in the array, which must be comparable.
     * @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.
     * @return The index of the key if found, otherwise -1.
     */
    @Override
    public <T extends Comparable<T>> int find(T[] array, T key) {
        if (array.length == 0) {
            throw new IllegalArgumentException("Input array must not be empty.");
        }
        if (!isSorted(array)) {
            throw new IllegalArgumentException("Input array must be sorted.");
        }
        if (key == null) {
            throw new IllegalArgumentException("Key must not be null.");
        }

        int fibMinus1 = 1;
        int fibMinus2 = 0;
        int fibNumber = fibMinus1 + fibMinus2;
        int n = array.length;

        while (fibNumber < n) {
            fibMinus2 = fibMinus1;
            fibMinus1 = fibNumber;
            fibNumber = fibMinus2 + fibMinus1;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check array.length > 0 (or !isEmpty()) before calling find() and short-circuit with -1 if empty.
  2. Ensure upstream data sources actually populate the array for non-empty result sets.
  3. Return a sentinel (e.g. -1) at your API boundary when the source list is empty rather than delegating to FibonacciSearch.

Example fix

// before
int idx = new FibonacciSearch().find(arr, key);

// after
int idx = arr.length == 0 ? -1 : new FibonacciSearch().find(arr, key);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || array.length == 0) return -1;

Prevention

When it happens

Trigger: Calling find(new Integer[]{}, key); passing a collection-backed array that was emptied before the call; filtering an array down to zero elements then searching.

Common situations: Reading search data from a query or stream that returned no rows; defaulting to an empty array when a config list is absent; test fixtures using Collections.emptyList().toArray().

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/79953148bf4637fa. Report an issue: GitHub.