TheAlgorithms/Java · error · IllegalArgumentException

Array must be non-empty.

Error message

Array must be non-empty.

What it means

Thrown by FindMin.findMin(int[] array) when the input array has length 0. The method initializes min to array[0] and iterates from index 1, so it requires at least one element. The guard prevents an ArrayIndexOutOfBoundsException on the empty array access.

Source

Thrown at src/main/java/com/thealgorithms/maths/FindMin.java:16

package com.thealgorithms.maths;

public final class FindMin {
    private FindMin() {
    }

    /**
     * @brief finds the minimum value stored in the input array
     *
     * @param array the input array
     * @exception IllegalArgumentException input array is empty
     * @return the mimum value stored in the input array
     */
    public static int findMin(final int[] array) {
        if (array.length == 0) {
            throw new IllegalArgumentException("Array must be non-empty.");
        }
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check array.length > 0 before calling findMin.
  2. Handle the empty case in business logic (return Optional, default value, or early-exit).
  3. Validate upstream data sources to ensure they populate arrays before forwarding.

Example fix

// before
int min = FindMin.findMin(data);

// after
if (data.length == 0) {
    return Optional.<Integer>empty();
}
int min = FindMin.findMin(data);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || array.length == 0) {
    throw new IllegalArgumentException("Array must be non-empty");
}
int min = FindMin.findMin(array);

Type guard

static boolean isNonEmpty(int[] array) {
    return array != null && array.length > 0;
}

Try / catch

try {
    int min = FindMin.findMin(array);
} catch (IllegalArgumentException e) {
    // empty array; provide default or rethrow
}

Prevention

When it happens

Trigger: Calling findMin(new int[0]) or any zero-length array. Empty arrays produced by filtering, splitting, or database/file reads that return no data.

Common situations: Processing datasets that may be empty after business-logic filtering. Test setups with unpopulated fixtures. Integrations with external APIs returning empty result sets.

Related errors


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