{"record":{"id":"40ce7fccb808b052","repo":"TheAlgorithms/Java","slug":"array-must-be-non-empty","errorCode":null,"errorMessage":"Array must be non-empty.","messagePattern":"Array must be non-empty\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FindMax.java","lineNumber":17,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class FindMax {\n    private FindMax() {\n    }\n\n    /**\n     * @brief finds the maximum value stored in the input array\n     *\n     * @param array the input array\n     * @exception IllegalArgumentException input array is empty\n     * @return the maximum value stored in the input array\n     */\n    public static int findMax(final int[] array) {\n        int n = array.length;\n        if (n == 0) {\n            throw new IllegalArgumentException(\"Array must be non-empty.\");\n        }\n        int max = array[0];\n        for (int i = 1; i < n; i++) {\n            if (array[i] > max) {\n                max = array[i];\n            }\n        }\n        return max;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FindMax.java#L1-L28","documentation":"Thrown by FindMax.findMax(int[] array) when the input array has length 0. The method iterates from index 1 comparing against array[0] as the initial max; without at least one element there is no valid starting maximum. The guard prevents an ArrayIndexOutOfBoundsException at array[0].","triggerScenarios":"Calling findMax(new int[0]) or passing any zero-length array. Also triggered if an upstream operation (filter, split, sublist) produces an empty array that is forwarded without a size check.","commonSituations":"Processing collections or streams that may legitimately be empty after filtering. Receiving arrays from external APIs, file parsing, or database queries that return zero rows. Test fixtures that forget to populate the array.","solutions":["Check array.length > 0 before calling findMax.","Return an Optional<Integer> from your own wrapper to handle the empty case gracefully.","Ensure upstream data sources never produce empty arrays, or handle the empty case explicitly in business logic."],"exampleFix":"// before\nint max = FindMax.findMax(data);\n\n// after\nif (data.length == 0) {\n    throw new IllegalStateException(\"Cannot find max of empty dataset\");\n}\nint max = FindMax.findMax(data);","handlingStrategy":"validation","validationCode":"if (array == null || array.length == 0) {\n    throw new IllegalArgumentException(\"Array must be non-empty\");\n}\nint max = FindMax.findMax(array);","typeGuard":"static boolean isNonEmpty(int[] array) {\n    return array != null && array.length > 0;\n}","tryCatchPattern":"try {\n    int max = FindMax.findMax(array);\n} catch (IllegalArgumentException e) {\n    // array was empty; provide default or rethrow\n}","preventionTips":["Check array.length > 0 before calling findMax.","Return Optional<Integer> from wrapper methods to handle emptiness idiomatically.","Validate data sources that may produce empty arrays."],"tags":["math","array","empty-collection","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}