TheAlgorithms/Java · error · IllegalArgumentException

Input array cannot be null

Error message

Input array cannot be null

What it means

Thrown by the PrefixSum constructor when the input array is null. An empty array (length 0) is accepted here — it just builds a prefixSums of size 1 — so only null is rejected, differing from DifferenceArray which also rejects empty.

Source

Thrown at src/main/java/com/thealgorithms/prefixsum/PrefixSum.java:28

 * <p>This implementation uses a long array for the prefix sums to prevent
 * integer overflow when the sum of elements exceeds Integer.MAX_VALUE.
 *
 * @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 PrefixSum {

    private final long[] prefixSums;

    /**
     * Constructor to preprocess the input array.
     *
     * @param array The input integer array.
     * @throws IllegalArgumentException if the array is null.
     */
    public PrefixSum(int[] array) {
        if (array == null) {
            throw new IllegalArgumentException("Input array cannot be null");
        }
        this.prefixSums = new long[array.length + 1];
        this.prefixSums[0] = 0;

        for (int i = 0; i < array.length; i++) {
            // Automatically promotes int to long during addition
            this.prefixSums[i + 1] = this.prefixSums[i] + array[i];
        }
    }

    /**
     * Calculates the sum of elements in the range [left, right].
     * Indices are 0-based.
     *
     * @param left  The starting index (inclusive).
     * @param right The ending index (inclusive).
     * @return The sum of elements from index left to right as a long.
     * @throws IndexOutOfBoundsException if indices are out of valid range.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Null-check the array at the caller and pass Collections.empty / a 0-length array or skip building if null.
  2. Ensure the producer never returns null — return new int[0] as a null-object instead.
  3. Use Optional or a requireNonNull wrapper before construction.

Example fix

// before
PrefixSum ps = new PrefixSum(arr); // arr may be null

// after
int[] safe = arr == null ? new int[0] : arr;
PrefixSum ps = new PrefixSum(safe);
Defensive patterns

Strategy: validation

Validate before calling

int[] safe = array == null ? new int[0] : array;
PrefixSum ps = new PrefixSum(safe);

Type guard

static boolean usableForPrefixSum(int[] a) {
    return a != null; // empty is allowed by PrefixSum
}

Try / catch

try {
    PrefixSum ps = new PrefixSum(array);
} catch (IllegalArgumentException e) {
    logger.warn("Null array passed to PrefixSum");
}

Prevention

When it happens

Trigger: new PrefixSum(null), or new PrefixSum(someField) where someField was never assigned.

Common situations: A deserialized object whose int[] field stayed null; a method returning null on failure being passed straight in; null propagated from an upstream JSON parse of a missing field.

Related errors


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