{"record":{"id":"f0631be5bed58aa9","repo":"TheAlgorithms/Java","slug":"input-array-cannot-be-null","errorCode":null,"errorMessage":"Input array cannot be null","messagePattern":"Input array cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java","lineNumber":21,"sourceCode":"import java.util.HashMap;\n\n@SuppressWarnings({\"rawtypes\", \"unchecked\"})\nfinal class LongestArithmeticSubsequence {\n    private LongestArithmeticSubsequence() {\n    }\n\n    /**\n     * Returns the length of the longest arithmetic subsequence in the given array.\n     *\n     * A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value\n     * (for 0 <= i < seq.length - 1).\n     *\n     * @param nums the input array of integers\n     * @return the length of the longest arithmetic subsequence\n     */\n    public static int getLongestArithmeticSubsequenceLength(int[] nums) {\n        if (nums == null) {\n            throw new IllegalArgumentException(\"Input array cannot be null\");\n        }\n\n        if (nums.length <= 1) {\n            return nums.length;\n        }\n\n        HashMap<Integer, Integer>[] dp = new HashMap[nums.length];\n        int maxLength = 2;\n\n        // fill the dp array\n        for (int i = 0; i < nums.length; i++) {\n            dp[i] = new HashMap<>();\n            for (int j = 0; j < i; j++) {\n                final int diff = nums[i] - nums[j];\n                dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);\n                maxLength = Math.max(maxLength, dp[i].get(diff));\n            }\n        }","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java#L3-L39","documentation":"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).","triggerScenarios":"Calling getLongestArithmeticSubsequenceLength(null) — from an uninitialized array variable, a failed deserialization, or an optional field that was never populated.","commonSituations":"Processing optional numeric inputs that default to null; JSON payloads where the array field is absent; test code that passes null to check behavior.","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."],"exampleFix":"// before\nint len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums); // throws if null\n\n// after\nint[] safeNums = Objects.requireNonNullElseGet(nums, () -> new int[0]);\nint len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(safeNums);","handlingStrategy":"validation","validationCode":"if (nums == null) {\n    throw new IllegalArgumentException(\"Input array must not be null\");\n}\nint len = LongestArithmeticSubsequence.getLongestArithmeticSubsequenceLength(nums);","typeGuard":"static boolean isNonNullArray(int[] nums) {\n    return nums != null;\n}","tryCatchPattern":null,"preventionTips":["Initialize array variables to new int[0] instead of null.","Use Objects.requireNonNull at the call boundary."],"tags":["arithmetic-subsequence","null-check","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}