TheAlgorithms/Java · error · IllegalArgumentException
Input array cannot be null
Error message
Input array cannot be null
What it means
getLongestArithmeticSubsequenceLength builds a HashMap per array element and iterates all pairs, so a null array would cause an NPE. The method rejects null with IllegalArgumentException immediately and returns the array length for arrays of size 0 or 1 (which are trivially arithmetic).
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java:21
import java.util.HashMap;
@SuppressWarnings({"rawtypes", "unchecked"})
final class LongestArithmeticSubsequence {
private LongestArithmeticSubsequence() {
}
/**
* Returns the length of the longest arithmetic subsequence in the given array.
*
* A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value
* (for 0 <= i < seq.length - 1).
*
* @param nums the input array of integers
* @return the length of the longest arithmetic subsequence
*/
public static int getLongestArithmeticSubsequenceLength(int[] nums) {
if (nums == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
if (nums.length <= 1) {
return nums.length;
}
HashMap<Integer, Integer>[] dp = new HashMap[nums.length];
int maxLength = 2;
// fill the dp array
for (int i = 0; i < nums.length; i++) {
dp[i] = new HashMap<>();
for (int j = 0; j < i; j++) {
final int diff = nums[i] - nums[j];
dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);
maxLength = Math.max(maxLength, dp[i].get(diff));
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Initialize the array to new int[0] instead of leaving it null.
- Guard with Objects.requireNonNull at the call boundary.
- If null means 'no data', return 0 or skip the computation.
Example fix
// before int len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums); // throws if null // after int[] safeNums = Objects.requireNonNullElseGet(nums, () -> new int[0]); int len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(safeNums);
Defensive patterns
Strategy: validation
Validate before calling
if (nums == null) {
throw new IllegalArgumentException("Input array must not be null");
}
int len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums); Type guard
static boolean isNonNullArray(int[] nums) {
return nums != null;
} Prevention
- Initialize array variables to new int[0] instead of null.
- Use Objects.requireNonNull at the call boundary.
When it happens
Trigger: Calling getLongestArithmeticSubsequenceLength(null) — from an uninitialized array variable, a failed deserialization, or an optional field that was never populated.
Common situations: Processing optional numeric inputs that default to null; JSON payloads where the array field is absent; test code that passes null to check behavior.
Related errors
- Cannot enqueue null data
- Cannot enqueue null item.
- Input strings must not be null.
- Weights and values must be non-null and of the same length.
- Input arrays cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f0631be5bed58aa9.
Report an issue: GitHub.