{"record":{"id":"ceabfe2441c4bbd3","repo":"TheAlgorithms/Java","slug":"values-array-cannot-be-empty-or-null","errorCode":null,"errorMessage":"Values array cannot be empty or null","messagePattern":"Values array cannot be empty or null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Median.java","lineNumber":38,"sourceCode":" */\npublic final class Median {\n    private Median() {\n    }\n\n    /**\n     * Calculates the median of an array of integers.\n     * The array is sorted internally, so the original order is not preserved.\n     * For arrays with an odd number of elements, returns the middle element.\n     * For arrays with an even number of elements, returns the average of the two\n     * middle elements.\n     *\n     * @param values the array of integers to find the median of (can be unsorted)\n     * @return the median value as a double\n     * @throws IllegalArgumentException if the input array is empty or null\n     */\n    public static double median(int[] values) {\n        if (values == null || values.length == 0) {\n            throw new IllegalArgumentException(\"Values array cannot be empty or null\");\n        }\n\n        Arrays.sort(values);\n        int length = values.length;\n        if (length % 2 == 0) {\n            return (values[length / 2] + values[length / 2 - 1]) / 2.0;\n        } else {\n            return values[length / 2];\n        }\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Median.java#L20-L50","documentation":"Thrown by median(int[] values) when the input array is null or empty. The median of zero elements is undefined. The method also sorts the array in place, so a null array would cause a NullPointerException at Arrays.sort without this guard. Note: this method mutates the input array.","triggerScenarios":"Calling Median.median(null) or Median.median(new int[0]). Also hit when an array is conditionally populated and ends up empty.","commonSituations":"Processing query results or stream-collected arrays that may be empty. Passing a filtered array where all elements were removed. Forgetting that median() sorts in place and may receive a shared array reference.","solutions":["Check that values != null && values.length > 0 before calling median","Handle the empty-array case at the data layer (return Optional.empty or a default)","If the caller needs to preserve the original order, pass a copy since median() sorts in place"],"exampleFix":"// before\ndouble m = Median.median(data);\n\n// after\nif (data == null || data.length == 0) {\n    throw new IllegalArgumentException(\"Cannot compute median of empty array\");\n}\ndouble m = Median.median(data);","handlingStrategy":"validation","validationCode":"if (values == null || values.length == 0) {\n    throw new IllegalArgumentException(\"Cannot compute median of empty or null array\");\n}\ndouble m = Median.median(values);","typeGuard":"static boolean hasElements(int[] arr) {\n    return arr != null && arr.length > 0;\n}","tryCatchPattern":"try {\n    double m = Median.median(values);\n} catch (IllegalArgumentException e) {\n    // handle empty/null input\n    return Optional.empty();\n}","preventionTips":["Median.median sorts the array in place — pass a copy if the caller needs the original order","Null arrays cause a different error path than empty arrays, so check both","Guard data-loading code to reject empty result sets before they reach the median call"],"tags":["math","validation","illegal-argument","statistics","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}