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

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'.

Source

Thrown at src/main/java/com/thealgorithms/graph/Dinic.java:39

 * @see <a href="https://en.wikipedia.org/wiki/Dinic%27s_algorithm">Wikipedia: Dinic's algorithm</a>
 */
public final class Dinic {
    private Dinic() {
    }

    /**
     * Computes the maximum flow from source to sink using Dinic's algorithm.
     *
     * @param capacity square capacity matrix (n x n); entries must be >= 0
     * @param source source vertex index in [0, n)
     * @param sink sink vertex index in [0, n)
     * @return the maximum flow value
     * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or
     *     indices 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 i = 0; i < n; i++) {
            if (capacity[i] == null || capacity[i].length != n) {
                throw new IllegalArgumentException("Capacity matrix must be square");
            }
            for (int j = 0; j < n; j++) {
                if (capacity[i][j] < 0) {
                    throw new IllegalArgumentException("Capacities must be non-negative");
                }
            }
        }
        if (source < 0 || sink < 0 || source >= n || sink >= n) {
            throw new IllegalArgumentException("Source and sink must be valid vertex indices");
        }
        if (source == sink) {
            return 0;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Build a non-null n x n capacity matrix (at least 1x1) before calling.
  2. Guard capacity != null && capacity.length > 0 at the boundary.
  3. For an empty graph, handle it explicitly instead of calling maxFlow.

Example fix

// before
int f = Dinic.maxFlow(cap, s, t);

// after
if (cap == null || cap.length == 0) throw new IllegalArgumentException("capacity required");
int f = Dinic.maxFlow(cap, s, t);
Defensive patterns

Strategy: validation

Validate before calling

if (capacity == null || capacity.length == 0) {
    throw new IllegalArgumentException("capacity matrix required");
}
Dinic.maxFlow(capacity, source, sink);

Type guard

capacity != null && capacity.length > 0

Prevention

When it happens

Trigger: Passing null for the capacity matrix; an empty n=0 graph; a matrix field left null after a failed build.

Common situations: Graph construction that returns null for empty graphs; reading a matrix from a missing config; tests with an uninitialized matrix.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/8c465c0b8218eb0c. Report an issue: GitHub.