{"record":{"id":"6522a87025ff19fb","repo":"TheAlgorithms/Java","slug":"weights-matrix-must-not-be-null-or-empty","errorCode":null,"errorMessage":"Weights matrix must not be null or empty","messagePattern":"Weights matrix must not be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/YensKShortestPaths.java","lineNumber":114,"sourceCode":"                }\n            }\n            if (candidates.isEmpty()) {\n                break;\n            }\n            shortestPaths.add(candidates.poll());\n        }\n\n        // Map to list of node indices for output\n        List<List<Integer>> result = new ArrayList<>(shortestPaths.size());\n        for (Path p : shortestPaths) {\n            result.add(new ArrayList<>(p.nodes));\n        }\n        return result;\n    }\n\n    private static void validate(int[][] weights, int src, int dst, int k) {\n        if (weights == null || weights.length == 0) {\n            throw new IllegalArgumentException(\"Weights matrix must not be null or empty\");\n        }\n        int n = weights.length;\n        for (int i = 0; i < n; i++) {\n            if (weights[i] == null || weights[i].length != n) {\n                throw new IllegalArgumentException(\"Weights matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                int val = weights[i][j];\n                if (val < NO_EDGE) {\n                    throw new IllegalArgumentException(\"Weights must be -1 (no edge) or >= 0\");\n                }\n            }\n        }\n        if (src < 0 || dst < 0 || src >= n || dst >= n) {\n            throw new IllegalArgumentException(\"Invalid src/dst indices\");\n        }\n        if (k < 1) {\n            throw new IllegalArgumentException(\"k must be >= 1\");","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/YensKShortestPaths.java#L96-L132","documentation":"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.","triggerScenarios":"Calling kShortestPaths(null, src, dst, k), kShortestPaths(new int[0][], ...), or passing a matrix constructed from an empty vertex set.","commonSituations":"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.","solutions":["Guard the caller: if (weights == null || weights.length == 0) return List.of(); before calling.","Ensure the graph loader always returns at least a 1x1 matrix for single-vertex graphs.","If src == dst on an empty graph, short-circuit with an empty path list instead of invoking the algorithm."],"exampleFix":"// before\nList<List<Integer>> paths = YensKShortestPaths.kShortestPaths(weights, 0, 1, 3);\n\n// after\nif (weights == null || weights.length == 0) {\n    return List.of();\n}\nList<List<Integer>> paths = YensKShortestPaths.kShortestPaths(weights, 0, 1, 3);","handlingStrategy":"validation","validationCode":"if (weights == null || weights.length == 0) {\n    return List.of(); // or throw, depending on desired semantics\n}\nYensKShortestPaths.kShortestPaths(weights, src, dst, k);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat an empty graph as a no-op at the data-loading layer rather than letting it reach the algorithm.","Ensure graph loaders return a minimum 1x1 matrix for single-vertex graphs instead of an empty array.","Distinguish 'no graph' (null) from 'empty graph' and handle each explicitly upstream."],"tags":["graph","validation","null-check","empty-input","yens"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}