TheAlgorithms/Java · error · IllegalArgumentException
Input array must not be null.
Error message
Input array must not be null.
What it means
Thrown by TwoPointers.isPairedSum when the input array is null. The two-pointer technique requires an actual array to scan from both ends; a null array would otherwise cause a NullPointerException on arr.length. This is a fast-fail precondition check converting a latent NPE into a meaningful contract violation.
Source
Thrown at src/main/java/com/thealgorithms/others/TwoPointers.java:25
* <p>
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
*/
public final class TwoPointers {
private TwoPointers() {
}
/**
* Checks whether there exists a pair of elements in a sorted array whose sum equals the specified key.
*
* @param arr a sorted array of integers in ascending order (must not be null)
* @param key the target sum to find
* @return {@code true} if there exists at least one pair whose sum equals {@code key}, {@code false} otherwise
* @throws IllegalArgumentException if {@code arr} is {@code null}
*/
public static boolean isPairedSum(int[] arr, int key) {
if (arr == null) {
throw new IllegalArgumentException("Input array must not be null.");
}
int left = 0;
int right = arr.length - 1;
while (left < right) {
int sum = arr[left] + arr[right];
if (sum == key) {
return true;
}
if (sum < key) {
left++;
} else {
right--;
}
}
return false;View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the caller passes a non-null int[] — initialize it to an empty array (new int[0]) if no elements exist.
- When the array comes from a Map or Optional, handle the absent case before calling isPairedSum.
- Add a null check or Objects.requireNonNull in the caller if the array origin is untrusted.
Example fix
// before
int[] arr = (int[]) map.get("pairs");
boolean found = TwoPointers.isPairedSum(arr, target);
// after
int[] arr = (int[]) map.getOrDefault("pairs", new int[0]);
boolean found = TwoPointers.isPairedSum(arr, target); Defensive patterns
Strategy: validation
Validate before calling
if (arr == null) {
arr = new int[0]; // or throw a domain-specific error upstream
}
boolean found = TwoPointers.isPairedSum(arr, key); Type guard
static boolean isUsable(int[] arr) {
return arr != null;
} Try / catch
try {
return TwoPointers.isPairedSum(arr, key);
} catch (IllegalArgumentException e) {
// arr was null; treat as "no pair found" only if that matches domain semantics
return false;
} Prevention
- Never pass map lookups directly into the API — resolve nullability first.
- Prefer returning an empty array from data-access methods instead of null.
- Annotate parameters with @Nullable/@NonNull and run a static analyzer.
When it happens
Trigger: Calling isPairedSum(null, key), or passing an array reference that was never assigned (left null after a failed lookup or a map.get that returned null).
Common situations: Array sourced from a Map.get that returned null because the key was absent, a List.toArray() result mishandled, an unconfigured field, or test code that forgets to initialize the input.
Related errors
- Cannot insert null element
- Cannot add null element to the list
- Cannot add null element to the list
- Element cannot be null
- Input lists and result collection must not be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/24da8cd87352ec97.
Report an issue: GitHub.