{"record":{"id":"6bd54d0c9e630106","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-not-be-null-or-empty-6bd54d","errorCode":null,"errorMessage":"Capacity matrix must not be null or empty","messagePattern":"Capacity matrix must not be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/EdmondsKarp.java","lineNumber":37,"sourceCode":" */\npublic final class EdmondsKarp {\n\n    private EdmondsKarp() {\n    }\n\n    /**\n     * Computes the maximum flow from {@code source} to {@code sink} in the provided capacity matrix.\n     *\n     * @param capacity the capacity matrix representing the directed graph; must be square and non-null\n     * @param source the source vertex index\n     * @param sink the sink vertex index\n     * @return the value of the maximum flow between {@code source} and {@code sink}\n     * @throws IllegalArgumentException if the matrix is {@code null}, not square, contains negative\n     *         capacities, or if {@code source} / {@code sink} indices are invalid\n     */\n    public static int maxFlow(int[][] capacity, int source, int sink) {\n        if (capacity == null || capacity.length == 0) {\n            throw new IllegalArgumentException(\"Capacity matrix must not be null or empty\");\n        }\n\n        final int n = capacity.length;\n        for (int row = 0; row < n; row++) {\n            if (capacity[row] == null || capacity[row].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int col = 0; col < n; col++) {\n                if (capacity[row][col] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n\n        if (source < 0 || source >= n || sink < 0 || sink >= n) {\n            throw new IllegalArgumentException(\"Source and sink must be valid vertex indices\");\n        }\n        if (source == sink) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/EdmondsKarp.java#L19-L55","documentation":"EdmondsKarp.maxFlow throws this IllegalArgumentException when the capacity matrix is null or has zero rows. It is the first guard in the method, ensuring a well-formed matrix exists before any squareness or index checks run.","triggerScenarios":"Calling EdmondsKarp.maxFlow(null, source, sink) or EdmondsKarp.maxFlow(new int[0][], source, sink).","commonSituations":"Capacity matrix not yet initialized when the call occurs. Parsing a graph from input that produced no vertices. A deserialization or config load that returned null for an empty graph.","solutions":["Guard for null or empty matrix before calling: if (cap == null || cap.length == 0) handle gracefully.","Ensure the graph construction step always produces at least one vertex for non-trivial flow problems.","Check upstream data loading that builds the matrix."],"exampleFix":"// before\nint flow = EdmondsKarp.maxFlow(cap, s, t); // cap may be null\n\n// after\nif (cap == null || cap.length == 0) {\n    return 0;\n}\nint flow = EdmondsKarp.maxFlow(cap, s, t);","handlingStrategy":"validation","validationCode":"if (capacity == null || capacity.length == 0) {\n    // handle empty graph case\n    return 0;\n}","typeGuard":"boolean hasMatrix(int[][] cap) {\n    return cap != null && cap.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Never pass a raw field that might be null; initialize to a minimal valid matrix.","Log when graph construction yields an empty matrix.","Return early for empty graphs at the caller level."],"tags":["graph-algorithm","argument-validation","null-check","max-flow"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}