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
- Null-check the array at the caller and pass Collections.empty / a 0-length array or skip building if null.
- Ensure the producer never returns null — return new int[0] as a null-object instead.
- 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
- Adopt the null-object pattern: producers return new int[0], never null.
- Centralize array construction so a single factory guarantees non-null.
- Note PrefixSum allows empty but DifferenceArray does not — don't assume uniform validation across classes.
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
- Input matrix cannot be null or empty
- Input array cannot be null
- Prefix sum array cannot be null
- Input array cannot be null
- Input array cannot be null or empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/97e64c47a0e1261a.
Report an issue: GitHub.