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

  1. Initialize the array to new int[0] instead of leaving it null.
  2. Guard with Objects.requireNonNull at the call boundary.
  3. 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

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


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/f0631be5bed58aa9. Report an issue: GitHub.