{"record":{"id":"959866f6ec72a820","repo":"TheAlgorithms/Java","slug":"input-array-cannot-be-null-959866","errorCode":null,"errorMessage":"Input array cannot be null","messagePattern":"Input array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/SubarraySumEqualsK.java","lineNumber":52,"sourceCode":" * @author Ruturaj Jadhav, <a href=\"https://github.com/ruturajjadhav07\">ruturajjadhav07</a>\n */\npublic final class SubarraySumEqualsK {\n\n    private SubarraySumEqualsK() {\n        // Utility class; prevent instantiation\n    }\n\n    /**\n     * Counts the number of subarrays whose sum equals k.\n     *\n     * @param nums The input integer array.\n     * @param k    The target sum.\n     * @return The number of continuous subarrays summing to k.\n     * @throws IllegalArgumentException if nums is null.\n     */\n    public static int countSubarrays(int[] nums, int k) {\n        if (nums == null) {\n            throw new IllegalArgumentException(\"Input array cannot be null\");\n        }\n\n        Map<Long, Integer> prefixSumFrequency = new HashMap<>();\n        prefixSumFrequency.put(0L, 1);\n\n        long prefixSum = 0;\n        int count = 0;\n\n        for (int num : nums) {\n            prefixSum += num;\n\n            long requiredSum = prefixSum - k;\n            count += prefixSumFrequency.getOrDefault(requiredSum, 0);\n\n            prefixSumFrequency.put(prefixSum, prefixSumFrequency.getOrDefault(prefixSum, 0) + 1);\n        }\n\n        return count;","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/SubarraySumEqualsK.java#L34-L70","documentation":"Thrown by SubarraySumEqualsK.countSubarrays(nums, k) when nums is null. The method uses a HashMap of prefix-sum frequencies and iterates nums with a for-each loop, so a null array would NPE during iteration without this guard. Empty arrays are allowed (returns 0).","triggerScenarios":"SubarraySumEqualsK.countSubarrays(null, 5), or passing an array field that was never initialized.","commonSituations":"A nullable int[] from a parsed request body; a service returning null on a not-found path fed into countSubarrays; test data not set up.","solutions":["Null-check nums at the caller; pass new int[0] for the empty case.","Make the data source return empty instead of null.","Wrap with Objects.requireNonNull(nums) if null is a programmer error."],"exampleFix":"// before\nint c = SubarraySumEqualsK.countSubarrays(arr, k); // arr may be null\n\n// after\nint c = SubarraySumEqualsK.countSubarrays(arr == null ? new int[0] : arr, k);","handlingStrategy":"validation","validationCode":"int[] safe = nums == null ? new int[0] : nums;\nint c = SubarraySumEqualsK.countSubarrays(safe, k);","typeGuard":"static boolean usable(int[] a) {\n    return a != null; // empty allowed, returns 0\n}","tryCatchPattern":"try {\n    int c = SubarraySumEqualsK.countSubarrays(nums, k);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Null nums in countSubarrays\");\n}","preventionTips":["Return empty arrays from producers instead of null.","Coalesce null at the request boundary before any analytics call.","Wrap with Objects.requireNonNull if null is a programmer error."],"tags":["prefix-sum","input-validation","illegal-argument","null-check","hashmap"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}