{"record":{"id":"e4b640e68e7e63bd","repo":"TheAlgorithms/Java","slug":"numbers-array-cannot-be-empty-or-null-e4b640","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/AbsoluteMin.java","lineNumber":15,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class AbsoluteMin {\n    private AbsoluteMin() {\n    }\n\n    /**\n     * Compares the numbers given as arguments to get the absolute min value.\n     *\n     * @param numbers The numbers to compare\n     * @return The absolute min value\n     */\n    public static int getMinValue(int... numbers) {\n        if (numbers == null || numbers.length == 0) {\n            throw new IllegalArgumentException(\"Numbers array cannot be empty or null\");\n        }\n\n        long absMin = numbers[0];\n        for (int i = 1; i < numbers.length; i++) {\n            long current = numbers[i];\n            if (Math.abs(current) < Math.abs(absMin) || (Math.abs(current) == Math.abs(absMin) && current < absMin)) {\n                absMin = current;\n            }\n        }\n        return (int) absMin;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/AbsoluteMin.java#L1-L28","documentation":"Thrown by AbsoluteMin.getMinValue when the varargs 'numbers' parameter is null or has length 0. The method needs at least one value to seed 'absMin' (numbers[0]); with an empty array that seed read would throw ArrayIndexOutOfBounds, so the guard converts that into a clear precondition failure. It is the only entry point to the class, so any caller passing no values hits it.","triggerScenarios":"Calling getMinValue() with zero arguments; passing (int[]) null; passing an int[] that was filtered down to length 0 (e.g. stream().filter().toArray() that matched nothing).","commonSituations":"Computing a minimum over a dynamic/filtered collection where the filter can yield nothing; unit tests that call the no-arg form; deserialized arrays that came back null; refactoring a fixed list into a stream pipeline that lost the empty case.","solutions":["Pass at least one element: getMinValue(7) or getMinValue(arr) with a non-empty arr.","Guard before calling: if (arr != null && arr.length > 0) { ... getMinValue(arr) ... } else { handle empty }.","Pick a default/early-return when the source collection is empty rather than forwarding it.","If the array may be null from an upstream API, null-check and coalesce to a sentinel or skip the call."],"exampleFix":"// before\nint m = AbsoluteMin.getMinValue(filtered.toArray());\n// after\nint[] arr = filtered.stream().mapToInt(Integer::intValue).toArray();\nif (arr.length == 0) { throw new IllegalStateException(\"no samples\"); }\nint m = AbsoluteMin.getMinValue(arr);","handlingStrategy":"validation","validationCode":"if (numbers == null || numbers.length == 0) {\n    throw new IllegalStateException(\"cannot compute absolute min of no values\");\n}\nint min = AbsoluteMin.getMinValue(numbers);","typeGuard":"static boolean hasValues(int... numbers) {\n    return numbers != null && numbers.length > 0;\n}","tryCatchPattern":"try {\n    int min = AbsoluteMin.getMinValue(arr);\n} catch (IllegalArgumentException e) {\n    // arr was null/empty; provide a default or report to caller\n    min = defaultValue;\n}","preventionTips":["Never forward a possibly-empty filtered collection to getMinValue; check size first.","Treat an empty collection as a domain-level condition, not an empty array to pass through.","Write a tiny helper that returns OptionalInt for the empty case."],"tags":["validation","varargs","maths","precondition","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}