TheAlgorithms/Java · error · IllegalArgumentException
Prefix sum array cannot be null
Error message
Prefix sum array cannot be null
What it means
Thrown by RangeSumQuery.sumRange(prefixSum, left, right) when prefixSum is null. This overload takes an externally-built prefix-sum array, so it must guard the reference itself. Note the inconsistent API shape: this null check throws IllegalArgumentException, not NullPointerException.
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java:66
for (int i = 0; i < n; i++) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
return prefixSum;
}
/**
* Returns the sum of elements from index left to right (inclusive)
* using the provided prefix sum array.
*
* @param prefixSum The prefix sum array computed using buildPrefixSum.
* @param left The start index (inclusive).
* @param right The end index (inclusive).
* @return The sum of elements in the range [left, right].
* @throws IllegalArgumentException if indices are invalid.
*/
public static int sumRange(int[] prefixSum, int left, int right) {
if (prefixSum == null) {
throw new IllegalArgumentException("Prefix sum array cannot be null");
}
if (left < 0 || right >= prefixSum.length - 1 || left > right) {
throw new IllegalArgumentException("Invalid range indices");
}
return prefixSum[right + 1] - prefixSum[left];
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the prefix-sum array passed is the one returned by buildPrefixSum and is non-null.
- Null-check before calling: if (prefixSum == null) re-build or skip.
- Treat a null prefix sum as a build error: rebuild from the source array instead of querying.
Example fix
// before int s = RangeSumQuery.sumRange(cachedPrefix, left, right); // cachedPrefix may be null // after if (cachedPrefix == null) cachedPrefix = RangeSumQuery.buildPrefixSum(source); int s = RangeSumQuery.sumRange(cachedPrefix, left, right);
Defensive patterns
Strategy: validation
Validate before calling
if (prefixSum == null) {
prefixSum = RangeSumQuery.buildPrefixSum(source); // rebuild
}
int s = RangeSumQuery.sumRange(prefixSum, left, right); Type guard
static boolean usablePrefix(int[] ps) {
return ps != null && ps.length >= 1;
} Try / catch
try {
int s = RangeSumQuery.sumRange(prefixSum, left, right);
} catch (IllegalArgumentException e) {
logger.warn("Null prefixSum passed to sumRange");
} Prevention
- Keep the source array and prefix sum together so you can rebuild if the cache is null.
- Treat a null prefix sum as a build failure, not a query failure.
- Prefer the PrefixSum class (encapsulates state) over the static RangeSumQuery when caching.
When it happens
Trigger: RangeSumQuery.sumRange(null, 0, 3), or passing a prefix-sum array from a different/older build that may be null.
Common situations: Reusing a cached prefix-sum field that was cleared to null; mixing this static API with a PrefixSum instance that exposed null; race condition clearing a shared array.
Related errors
- Input array cannot be null
- Input array cannot be null
- Input matrix cannot be null or empty
- 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/35bdcbcca52fc036.
Report an issue: GitHub.