{"record":{"id":"8c465c0b8218eb0c","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-not-be-null-or-empty","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/Dinic.java","lineNumber":39,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Dinic%27s_algorithm\">Wikipedia: Dinic's algorithm</a>\n */\npublic final class Dinic {\n    private Dinic() {\n    }\n\n    /**\n     * Computes the maximum flow from source to sink using Dinic's algorithm.\n     *\n     * @param capacity square capacity matrix (n x n); entries must be >= 0\n     * @param source source vertex index in [0, n)\n     * @param sink sink vertex index in [0, n)\n     * @return the maximum flow value\n     * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or\n     *     indices 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        final int n = capacity.length;\n        for (int i = 0; i < n; i++) {\n            if (capacity[i] == null || capacity[i].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                if (capacity[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n        if (source < 0 || sink < 0 || source >= n || sink >= n) {\n            throw new IllegalArgumentException(\"Source and sink must be valid vertex indices\");\n        }\n        if (source == sink) {\n            return 0;\n        }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/Dinic.java#L21-L57","documentation":"Thrown by Dinic.maxFlow(int[][] capacity, int source, int sink) when capacity is null or has zero rows. The algorithm uses capacity.length as the vertex count n; null or empty makes the matrix meaningless. Message: 'Capacity matrix must not be null or empty'.","triggerScenarios":"Passing null for the capacity matrix; an empty n=0 graph; a matrix field left null after a failed build.","commonSituations":"Graph construction that returns null for empty graphs; reading a matrix from a missing config; tests with an uninitialized matrix.","solutions":["Build a non-null n x n capacity matrix (at least 1x1) before calling.","Guard capacity != null && capacity.length > 0 at the boundary.","For an empty graph, handle it explicitly instead of calling maxFlow."],"exampleFix":"// before\nint f = Dinic.maxFlow(cap, s, t);\n\n// after\nif (cap == null || cap.length == 0) throw new IllegalArgumentException(\"capacity required\");\nint f = Dinic.maxFlow(cap, s, t);","handlingStrategy":"validation","validationCode":"if (capacity == null || capacity.length == 0) {\n    throw new IllegalArgumentException(\"capacity matrix required\");\n}\nDinic.maxFlow(capacity, source, sink);","typeGuard":"capacity != null && capacity.length > 0","tryCatchPattern":null,"preventionTips":["Allocate capacity as new int[n][n] for the known vertex count.","Handle empty graphs explicitly rather than calling maxFlow.","Validate at the graph-build boundary."],"tags":["null-check","graph","input-validation","matrix"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}