{"record":{"id":"64d9b4230ddc1f1d","repo":"TheAlgorithms/Java","slug":"numbers-array-cannot-be-empty-or-null","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/AbsoluteMax.java","lineNumber":16,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class AbsoluteMax {\n    private AbsoluteMax() {\n    }\n\n    /**\n     * Finds the absolute maximum value among the given numbers.\n     *\n     * @param numbers The numbers to compare.\n     * @return The absolute maximum value.\n     * @throws IllegalArgumentException If the input array is empty or null.\n     */\n    public static int getMaxValue(int... numbers) {\n        if (numbers == null || numbers.length == 0) {\n            throw new IllegalArgumentException(\"Numbers array cannot be empty or null\");\n        }\n        int absMax = numbers[0];\n        for (int i = 1; i < numbers.length; i++) {\n            if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {\n                absMax = numbers[i];\n            }\n        }\n        return absMax;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/AbsoluteMax.java#L1-L27","documentation":"AbsoluteMax.getMaxValue(varargs int...) rejects null or empty input because there is no maximum of zero numbers — the loop starts at numbers[0]. A null varargs array (possible when explicitly passing null) or zero arguments would cause a meaningless result or IndexOutOfBoundsException. The check fails fast with a clear contract.","triggerScenarios":"Calling getMaxValue() with no arguments, getMaxValue(null), or passing a null array reference explicitly.","commonSituations":"Passing a possibly-empty collection converted to an array (e.g. list.stream().mapToInt(...).toArray() on an empty list yields an empty array), calling getMaxValue() with no args by mistake, or forwarding a null array from an upstream computation.","solutions":["Check the array is non-null and non-empty before calling; provide a default or error for empty input.","When converting from a collection, guard: if (list.isEmpty()) return defaultValue;.","Never call getMaxValue(null); if null is possible, handle it explicitly before the call."],"exampleFix":"// before\nint max = AbsoluteMax.getMaxValue(arr); // arr may be empty\n\n// after\nif (arr == null || arr.length == 0) {\n    throw new IllegalArgumentException(\"need at least one value\");\n}\nint max = AbsoluteMax.getMaxValue(arr);","handlingStrategy":"validation","validationCode":"if (numbers == null || numbers.length == 0) {\n    throw new IllegalArgumentException(\"numbers must be non-null and non-empty\");\n}\nAbsoluteMax.getMaxValue(numbers);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard empty collections before converting to arrays and passing to varargs methods.","Never pass null explicitly to a varargs parameter; handle null upstream.","Provide a sensible default or skip the call when the input set is empty."],"tags":["math","validation","null-check","empty-input","varargs","absolute-max"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}