{"record":{"id":"100657a89a478029","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-not-be-null-or-empty-100657","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/PushRelabel.java","lineNumber":145,"sourceCode":"        }\n    }\n\n    private static void relabel(int u, int[][] residual, int[] height) {\n        final int n = residual.length;\n        int minHeight = Integer.MAX_VALUE;\n        for (int v = 0; v < n; v++) {\n            if (residual[u][v] > 0) {\n                minHeight = Math.min(minHeight, height[v]);\n            }\n        }\n        if (minHeight < Integer.MAX_VALUE) {\n            height[u] = minHeight + 1;\n        }\n    }\n\n    private static void validate(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        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    }\n}\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/PushRelabel.java#L127-L163","documentation":"PushRelabel.validate throws this IllegalArgumentException when the capacity matrix is null or has zero rows. It is the first guard before the push-relabel maximum flow computation, ensuring the matrix exists.","triggerScenarios":"Calling PushRelabel with a null capacity array or new int[0][].","commonSituations":"Empty graph parsed from input. Matrix uninitialized after a failed build step. Null returned from a graph loader.","solutions":["Null/empty-check the matrix and return 0 flow before calling.","Ensure the graph has at least one vertex.","Log upstream when the loader produces an empty matrix."],"exampleFix":"// before\nint flow = PushRelabel.maxFlow(cap, s, t);\n\n// after\nif (cap == null || cap.length == 0) return 0;\nint flow = PushRelabel.maxFlow(cap, s, t);","handlingStrategy":"validation","validationCode":"if (capacity == null || capacity.length == 0) {\n    return 0;\n}","typeGuard":"boolean hasMatrix(int[][] cap) { return cap != null && cap.length > 0; }","tryCatchPattern":null,"preventionTips":["Return 0 flow for empty graphs at the caller.","Ensure graph construction yields at least one vertex.","Log empty matrices to catch upstream bugs."],"tags":["graph-algorithm","argument-validation","null-check","max-flow","push-relabel"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}