{"record":{"id":"2b809579e1cdeb7a","repo":"TheAlgorithms/Java","slug":"numbers-array-cannot-be-empty-or-null-2b8095","errorCode":null,"errorMessage":"Numbers array cannot be empty or null","messagePattern":"Numbers array cannot be empty or null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Average.java","lineNumber":34,"sourceCode":"\n    // Prevent instantiation of this utility class\n    private Average() {\n        throw new UnsupportedOperationException(\"This is a utility class and cannot be instantiated.\");\n    }\n\n    /**\n     * Computes the arithmetic mean of a {@code double} array.\n     *\n     * <p>The average is calculated as the sum of all elements divided\n     * by the number of elements: {@code avg = Σ(numbers[i]) / n}.\n     *\n     * @param numbers a non-null, non-empty array of {@code double} values\n     * @return the arithmetic mean of the given numbers\n     * @throws IllegalArgumentException if {@code numbers} is {@code null} or empty\n     */\n    public static double average(double[] numbers) {\n        if (numbers == null || numbers.length == 0) {\n            throw new IllegalArgumentException(\"Numbers array cannot be empty or null\");\n        }\n        double sum = 0;\n        for (double number : numbers) {\n            sum += number;\n        }\n        return sum / numbers.length;\n    }\n\n    /**\n     * Computes the arithmetic mean of an {@code int} array.\n     *\n     * <p>The sum is accumulated in a {@code long} to prevent integer overflow\n     * when processing large arrays or large values.\n     *\n     * @param numbers a non-null, non-empty array of {@code int} values\n     * @return the arithmetic mean as a {@code long} (truncated toward zero)\n     * @throws IllegalArgumentException if {@code numbers} is {@code null} or empty\n     */","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Average.java#L16-L52","documentation":"Thrown by Average.average(double[]) when the input array is null or has zero length. This guard prevents a division-by-zero (sum / numbers.length where length is 0) and a NullPointerException on the enhanced-for loop when numbers is null. The error message is shared with the int[] overload.","triggerScenarios":"Calling Average.average((double[]) null), Average.average(new double[0]), or passing a list that was converted to an empty array (e.g., list.stream().mapToDouble(Double::doubleValue).toArray() on an empty list).","commonSituations":"A collection or stream that was expected to contain elements but was empty due to a filtering condition that removed all items. A nullable field that was not initialized before being passed. JSON deserialization that produced a null array for a missing field.","solutions":["Check for null or empty before calling: if (numbers != null && numbers.length > 0).","Use the alternative method Average.averageStream(numbers) which returns OptionalDouble.empty() instead of throwing for null/empty input.","Fix the upstream data source to ensure the array is populated before the call."],"exampleFix":"// before\nAverage.average(new double[0]); // throws\nAverage.average(null);          // throws\n\n// after (use the stream-based API for safe empty handling)\nOptionalDouble result = Average.averageStream(numbers);\ndouble mean = result.orElse(0.0); // or orElseThrow with domain-specific error","handlingStrategy":"validation","validationCode":"// Check for null or empty before calling average(double[])\nif (numbers == null || numbers.length == 0) {\n    // handle gracefully: return default, log, or throw domain-specific error\n    return 0.0;\n}\ndouble mean = Average.average(numbers);\n// Or use the stream-based alternative that returns OptionalDouble:\nOptionalDouble result = Average.averageStream(numbers);\ndouble mean = result.orElse(0.0);","typeGuard":"static boolean hasElements(double[] arr) {\n    return arr != null && arr.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Prefer Average.averageStream() for potentially-empty inputs — it returns OptionalDouble instead of throwing.","Validate arrays at the data boundary (after parsing, before computation).","If using Java Streams, check isEmpty() on the source collection before converting to array."],"tags":["input-validation","null-check","empty-array","average","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}