TheAlgorithms/Java · error · IllegalArgumentException
Capacity matrix must not be null or empty
Error message
Capacity matrix must not be null or empty
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/graph/EdmondsKarp.java:37
*/
public final class EdmondsKarp {
private EdmondsKarp() {
}
/**
* Computes the maximum flow from {@code source} to {@code sink} in the provided capacity matrix.
*
* @param capacity the capacity matrix representing the directed graph; must be square and non-null
* @param source the source vertex index
* @param sink the sink vertex index
* @return the value of the maximum flow between {@code source} and {@code sink}
* @throws IllegalArgumentException if the matrix is {@code null}, not square, contains negative
* capacities, or if {@code source} / {@code sink} indices are invalid
*/
public static int maxFlow(int[][] capacity, int source, int sink) {
if (capacity == null || capacity.length == 0) {
throw new IllegalArgumentException("Capacity matrix must not be null or empty");
}
final int n = capacity.length;
for (int row = 0; row < n; row++) {
if (capacity[row] == null || capacity[row].length != n) {
throw new IllegalArgumentException("Capacity matrix must be square");
}
for (int col = 0; col < n; col++) {
if (capacity[row][col] < 0) {
throw new IllegalArgumentException("Capacities must be non-negative");
}
}
}
if (source < 0 || source >= n || sink < 0 || sink >= n) {
throw new IllegalArgumentException("Source and sink must be valid vertex indices");
}
if (source == sink) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int flow = EdmondsKarp.maxFlow(cap, s, t); // cap may be null
// after
if (cap == null || cap.length == 0) {
return 0;
}
int flow = EdmondsKarp.maxFlow(cap, s, t); Defensive patterns
Strategy: validation
Validate before calling
if (capacity == null || capacity.length == 0) {
// handle empty graph case
return 0;
} Type guard
boolean hasMatrix(int[][] cap) {
return cap != null && cap.length > 0;
} Prevention
- 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.
When it happens
Trigger: Calling EdmondsKarp.maxFlow(null, source, sink) or EdmondsKarp.maxFlow(new int[0][], source, sink).
Common situations: 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.
Related errors
- Capacity matrix must not be null or empty
- Source and sink must be valid vertex indices
- Capacity matrix must be square
- Capacities must be non-negative
- Source and sink must be valid vertex indices
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6bd54d0c9e630106.
Report an issue: GitHub.