{"record":{"id":"24da8cd87352ec97","repo":"TheAlgorithms/Java","slug":"input-array-must-not-be-null-24da8c","errorCode":null,"errorMessage":"Input array must not be null.","messagePattern":"Input array must not be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/TwoPointers.java","lineNumber":25,"sourceCode":" * <p>\n * Link: https://www.geeksforgeeks.org/two-pointers-technique/\n */\npublic final class TwoPointers {\n\n    private TwoPointers() {\n    }\n\n    /**\n     * Checks whether there exists a pair of elements in a sorted array whose sum equals the specified key.\n     *\n     * @param arr a sorted array of integers in ascending order (must not be null)\n     * @param key the target sum to find\n     * @return {@code true} if there exists at least one pair whose sum equals {@code key}, {@code false} otherwise\n     * @throws IllegalArgumentException if {@code arr} is {@code null}\n     */\n    public static boolean isPairedSum(int[] arr, int key) {\n        if (arr == null) {\n            throw new IllegalArgumentException(\"Input array must not be null.\");\n        }\n\n        int left = 0;\n        int right = arr.length - 1;\n\n        while (left < right) {\n            int sum = arr[left] + arr[right];\n\n            if (sum == key) {\n                return true;\n            }\n            if (sum < key) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n        return false;","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/TwoPointers.java#L7-L43","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nint[] arr = (int[]) map.get(\"pairs\");\nboolean found = TwoPointers.isPairedSum(arr, target);\n// after\nint[] arr = (int[]) map.getOrDefault(\"pairs\", new int[0]);\nboolean found = TwoPointers.isPairedSum(arr, target);","handlingStrategy":"validation","validationCode":"if (arr == null) {\n    arr = new int[0]; // or throw a domain-specific error upstream\n}\nboolean found = TwoPointers.isPairedSum(arr, key);","typeGuard":"static boolean isUsable(int[] arr) {\n    return arr != null;\n}","tryCatchPattern":"try {\n    return TwoPointers.isPairedSum(arr, key);\n} catch (IllegalArgumentException e) {\n    // arr was null; treat as \"no pair found\" only if that matches domain semantics\n    return false;\n}","preventionTips":["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."],"tags":["java","null-check","two-pointers","precondition","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}