TheAlgorithms/Java · error · IllegalArgumentException
Input array cannot be null
Error message
Input array cannot be null
What it means
Thrown by RangeSumQuery.buildPrefixSum(nums) when nums is null. Unlike PrefixSum, this is a static utility that returns a fresh prefix-sum array; null input has no length to iterate. Empty arrays (length 0) are allowed and yield a size-1 prefix array.
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java:43
*
* @author Ruturaj Jadhav, <a href="https://github.com/ruturajjadhav07">ruturajjadhav07</a>
*/
public final class RangeSumQuery {
private RangeSumQuery() {
// Utility class; prevent instantiation
}
/**
* Computes the prefix sum array for efficient range queries.
*
* @param nums The input integer array.
* @return Prefix sum array where prefixSum[i+1] = sum of nums[0..i].
* @throws IllegalArgumentException if nums is null.
*/
public static int[] buildPrefixSum(int[] nums) {
if (nums == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
int n = nums.length;
int[] prefixSum = new int[n + 1];
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].View on GitHub (pinned to fdfb9a395b)
Solutions
- Null-check at the caller; pass new int[0] if emptiness is acceptable.
- Make the producer return an empty array instead of null (null-object pattern).
- Wrap with Objects.requireNonNull(nums, "nums") for a clearer NPE if null is truly a bug.
Example fix
// before int[] ps = RangeSumQuery.buildPrefixSum(data); // data may be null // after int[] safe = data == null ? new int[0] : data; int[] ps = RangeSumQuery.buildPrefixSum(safe);
Defensive patterns
Strategy: validation
Validate before calling
int[] safe = nums == null ? new int[0] : nums; int[] ps = RangeSumQuery.buildPrefixSum(safe);
Type guard
static boolean usable(int[] a) {
return a != null; // empty allowed
} Try / catch
try {
int[] ps = RangeSumQuery.buildPrefixSum(nums);
} catch (IllegalArgumentException e) {
logger.warn("Null input to buildPrefixSum");
} Prevention
- Make data sources return empty arrays, not null.
- Coalesce null to empty at the boundary so downstream static utilities never see null.
- Wrap with Objects.requireNonNull if null indicates a real bug.
When it happens
Trigger: RangeSumQuery.buildPrefixSum(null), or buildPrefixSum(someArray) where someArray was never assigned.
Common situations: Passing a possibly-null field from a record/DTO; chaining a method that returns null on failure into buildPrefixSum; missing JSON field deserializing to null.
Related errors
- Prefix sum 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/543ac28d19c997e2.
Report an issue: GitHub.