TheAlgorithms/Java · error · IllegalArgumentException

Weights matrix must not be null or empty

Error message

Weights matrix must not be null or empty

What it means

YensKShortestPaths.kShortestPaths takes an adjacency matrix of edge weights; an empty or null matrix has no vertices to route between, so the algorithm cannot even determine n. The check rejects this before any Dijkstra sub-call would NPE or loop forever.

Source

Thrown at src/main/java/com/thealgorithms/graph/YensKShortestPaths.java:114

                }
            }
            if (candidates.isEmpty()) {
                break;
            }
            shortestPaths.add(candidates.poll());
        }

        // Map to list of node indices for output
        List<List<Integer>> result = new ArrayList<>(shortestPaths.size());
        for (Path p : shortestPaths) {
            result.add(new ArrayList<>(p.nodes));
        }
        return result;
    }

    private static void validate(int[][] weights, int src, int dst, int k) {
        if (weights == null || weights.length == 0) {
            throw new IllegalArgumentException("Weights matrix must not be null or empty");
        }
        int n = weights.length;
        for (int i = 0; i < n; i++) {
            if (weights[i] == null || weights[i].length != n) {
                throw new IllegalArgumentException("Weights matrix must be square");
            }
            for (int j = 0; j < n; j++) {
                int val = weights[i][j];
                if (val < NO_EDGE) {
                    throw new IllegalArgumentException("Weights must be -1 (no edge) or >= 0");
                }
            }
        }
        if (src < 0 || dst < 0 || src >= n || dst >= n) {
            throw new IllegalArgumentException("Invalid src/dst indices");
        }
        if (k < 1) {
            throw new IllegalArgumentException("k must be >= 1");

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard the caller: if (weights == null || weights.length == 0) return List.of(); before calling.
  2. Ensure the graph loader always returns at least a 1x1 matrix for single-vertex graphs.
  3. If src == dst on an empty graph, short-circuit with an empty path list instead of invoking the algorithm.

Example fix

// before
List<List<Integer>> paths = YensKShortestPaths.kShortestPaths(weights, 0, 1, 3);

// after
if (weights == null || weights.length == 0) {
    return List.of();
}
List<List<Integer>> paths = YensKShortestPaths.kShortestPaths(weights, 0, 1, 3);
Defensive patterns

Strategy: validation

Validate before calling

if (weights == null || weights.length == 0) {
    return List.of(); // or throw, depending on desired semantics
}
YensKShortestPaths.kShortestPaths(weights, src, dst, k);

Prevention

When it happens

Trigger: Calling kShortestPaths(null, src, dst, k), kShortestPaths(new int[0][], ...), or passing a matrix constructed from an empty vertex set.

Common situations: Empty graph loaded from a file/stream that had no edges or vertices, a graph builder that returns an empty array when the input collection is empty, or a null passed due to an upstream parse failure that wasn't checked.

Related errors


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