TheAlgorithms/Java · error · IllegalArgumentException

Input array cannot be null or empty.

Error message

Input array cannot be null or empty.

What it means

Thrown by the DifferenceArray constructor when inputArray is null or has length 0. The class needs at least one element to build its internal difference array (size n+1), and dereferences inputArray[0] immediately, so empty/null input has no valid representation.

Source

Thrown at src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java:36

 *
 * @see <a href="https://en.wikipedia.org/wiki/Finite_difference">Finite Difference (Wikipedia)</a>
 * @see <a href="https://en.wikipedia.org/wiki/Prefix_sum">Prefix Sum (Wikipedia)</a>
 * @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
 */
public class DifferenceArray {

    private final long[] differenceArray;
    private final int n;

    /**
     * Initializes the Difference Array from a given integer array.
     *
     * @param inputArray The initial array. Cannot be null or empty.
     * @throws IllegalArgumentException if the input array is null or empty.
     */
    public DifferenceArray(int[] inputArray) {
        if (inputArray == null || inputArray.length == 0) {
            throw new IllegalArgumentException("Input array cannot be null or empty.");
        }
        this.n = inputArray.length;
        // Size n + 1 allows for branchless updates at the right boundary (r + 1).
        this.differenceArray = new long[n + 1];
        initializeDifferenceArray(inputArray);
    }

    private void initializeDifferenceArray(int[] inputArray) {
        differenceArray[0] = inputArray[0];
        for (int i = 1; i < n; i++) {
            differenceArray[i] = inputArray[i] - inputArray[i - 1];
        }
    }

    /**
     * Adds a value to all elements in the range [l, r].
     *
     * <p>

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check inputArray != null && inputArray.length > 0 before constructing; handle the empty case at the caller (skip, default, or report).
  2. If emptiness is expected, branch on inputArray.length == 0 and skip DifferenceArray entirely — the structure is meaningless for empty input.
  3. Trace the source producing the array (file parse, DB query) and ensure it yields at least one element.

Example fix

// before
DifferenceArray da = new DifferenceArray(values); // values may be empty

// after
if (values == null || values.length == 0) {
    return; // or throw a domain-specific exception
}
DifferenceArray da = new DifferenceArray(values);
Defensive patterns

Strategy: validation

Validate before calling

if (inputArray == null || inputArray.length == 0) {
    throw new IllegalArgumentException("inputArray must be non-null and non-empty");
}
DifferenceArray da = new DifferenceArray(inputArray);

Type guard

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

Try / catch

try {
    DifferenceArray da = new DifferenceArray(inputArray);
} catch (IllegalArgumentException e) {
    // empty input is a domain condition, not a bug — handle by skipping the update phase
    return Collections.emptyList();
}

Prevention

When it happens

Trigger: new DifferenceArray(null), new DifferenceArray(new int[0]), or new DifferenceArray(someList.stream().mapToInt(...).toArray()) when the source collection is empty.

Common situations: Reading an array from JSON/CSV that parsed to nothing; a filtered stream yielding zero elements; a query result set that returned no rows before being mapped to int[].

Related errors


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