{"record":{"id":"75d3ff01a72ce138","repo":"TheAlgorithms/Java","slug":"array-must-be-non-empty-75d3ff","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/FindMin.java","lineNumber":16,"sourceCode":"package com.thealgorithms.maths;\n\npublic final class FindMin {\n    private FindMin() {\n    }\n\n    /**\n     * @brief finds the minimum value stored in the input array\n     *\n     * @param array the input array\n     * @exception IllegalArgumentException input array is empty\n     * @return the mimum value stored in the input array\n     */\n    public static int findMin(final int[] array) {\n        if (array.length == 0) {\n            throw new IllegalArgumentException(\"Array must be non-empty.\");\n        }\n        int min = array[0];\n        for (int i = 1; i < array.length; i++) {\n            if (array[i] < min) {\n                min = array[i];\n            }\n        }\n        return min;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FindMin.java#L1-L27","documentation":"Thrown by FindMin.findMin(int[] array) when the input array has length 0. The method initializes min to array[0] and iterates from index 1, so it requires at least one element. The guard prevents an ArrayIndexOutOfBoundsException on the empty array access.","triggerScenarios":"Calling findMin(new int[0]) or any zero-length array. Empty arrays produced by filtering, splitting, or database/file reads that return no data.","commonSituations":"Processing datasets that may be empty after business-logic filtering. Test setups with unpopulated fixtures. Integrations with external APIs returning empty result sets.","solutions":["Check array.length > 0 before calling findMin.","Handle the empty case in business logic (return Optional, default value, or early-exit).","Validate upstream data sources to ensure they populate arrays before forwarding."],"exampleFix":"// before\nint min = FindMin.findMin(data);\n\n// after\nif (data.length == 0) {\n    return Optional.<Integer>empty();\n}\nint min = FindMin.findMin(data);","handlingStrategy":"validation","validationCode":"if (array == null || array.length == 0) {\n    throw new IllegalArgumentException(\"Array must be non-empty\");\n}\nint min = FindMin.findMin(array);","typeGuard":"static boolean isNonEmpty(int[] array) {\n    return array != null && array.length > 0;\n}","tryCatchPattern":"try {\n    int min = FindMin.findMin(array);\n} catch (IllegalArgumentException e) {\n    // empty array; provide default or rethrow\n}","preventionTips":["Check array.length > 0 before calling findMin.","Handle empty datasets with Optional or default values.","Validate data pipelines 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"}