TheAlgorithms/Java · error · IllegalArgumentException
Input array cannot be null
Error message
Input array cannot be null
What it means
Thrown by SubarraySumEqualsK.countSubarrays(nums, k) when nums is null. The method uses a HashMap of prefix-sum frequencies and iterates nums with a for-each loop, so a null array would NPE during iteration without this guard. Empty arrays are allowed (returns 0).
Source
Thrown at src/main/java/com/thealgorithms/prefixsum/SubarraySumEqualsK.java:52
* @author Ruturaj Jadhav, <a href="https://github.com/ruturajjadhav07">ruturajjadhav07</a>
*/
public final class SubarraySumEqualsK {
private SubarraySumEqualsK() {
// Utility class; prevent instantiation
}
/**
* Counts the number of subarrays whose sum equals k.
*
* @param nums The input integer array.
* @param k The target sum.
* @return The number of continuous subarrays summing to k.
* @throws IllegalArgumentException if nums is null.
*/
public static int countSubarrays(int[] nums, int k) {
if (nums == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
Map<Long, Integer> prefixSumFrequency = new HashMap<>();
prefixSumFrequency.put(0L, 1);
long prefixSum = 0;
int count = 0;
for (int num : nums) {
prefixSum += num;
long requiredSum = prefixSum - k;
count += prefixSumFrequency.getOrDefault(requiredSum, 0);
prefixSumFrequency.put(prefixSum, prefixSumFrequency.getOrDefault(prefixSum, 0) + 1);
}
return count;View on GitHub (pinned to fdfb9a395b)
Solutions
- Null-check nums at the caller; pass new int[0] for the empty case.
- Make the data source return empty instead of null.
- Wrap with Objects.requireNonNull(nums) if null is a programmer error.
Example fix
// before int c = SubarraySumEqualsK.countSubarrays(arr, k); // arr may be null // after int c = SubarraySumEqualsK.countSubarrays(arr == null ? new int[0] : arr, k);
Defensive patterns
Strategy: validation
Validate before calling
int[] safe = nums == null ? new int[0] : nums; int c = SubarraySumEqualsK.countSubarrays(safe, k);
Type guard
static boolean usable(int[] a) {
return a != null; // empty allowed, returns 0
} Try / catch
try {
int c = SubarraySumEqualsK.countSubarrays(nums, k);
} catch (IllegalArgumentException e) {
logger.warn("Null nums in countSubarrays");
} Prevention
- Return empty arrays from producers instead of null.
- Coalesce null at the request boundary before any analytics call.
- Wrap with Objects.requireNonNull if null is a programmer error.
When it happens
Trigger: SubarraySumEqualsK.countSubarrays(null, 5), or passing an array field that was never initialized.
Common situations: A nullable int[] from a parsed request body; a service returning null on a not-found path fed into countSubarrays; test data not set up.
Related errors
- Input array cannot be null
- Input matrix cannot be null or empty
- Input array cannot be null
- Prefix sum 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/959866f6ec72a820.
Report an issue: GitHub.