{"record":{"id":"f8b53057a8324c99","repo":"TheAlgorithms/Java","slug":"number-of-vertices-must-be-positive","errorCode":null,"errorMessage":"Number of vertices must be positive","messagePattern":"Number of vertices must be positive","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java","lineNumber":47,"sourceCode":"        }\n    }\n\n    /**\n     * Represents the graph\n     */\n    static class Graph {\n        final int vertex;\n        final List<Edge> edges;\n\n        /**\n         * Constructor for the graph\n         *\n         * @param vertex number of vertices\n         * @param edges  list of edges\n         */\n        Graph(final int vertex, final List<Edge> edges) {\n            if (vertex < 0) {\n                throw new IllegalArgumentException(\"Number of vertices must be positive\");\n            }\n            if (edges == null || edges.isEmpty()) {\n                throw new IllegalArgumentException(\"Edges list must not be null or empty\");\n            }\n            for (final var edge : edges) {\n                checkEdgeVertices(edge.src, vertex);\n                checkEdgeVertices(edge.dest, vertex);\n            }\n\n            this.vertex = vertex;\n            this.edges = edges;\n        }\n    }\n\n    /**\n     * Represents a subset for Union-Find operations\n     */\n    private static class Component {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java#L29-L65","documentation":"Thrown by the Graph constructor in BoruvkaAlgorithm when the vertex count is negative. The constructor guards `vertex < 0` before allocating the graph, so a negative value is rejected up front. Note the message says 'positive' but the guard only blocks negatives, so 0 passes this check (it fails later during edge validation).","triggerScenarios":"Constructing `new BoruvkaAlgorithm.Graph(vertex, edges)` where `vertex` is a negative int, e.g. a count derived from `list.size() - 1` on an empty list, or read from malformed input/config.","commonSituations":"Reading vertex counts from user input or files where the value is -1/missing; off-by-one when computing counts; parse errors that yield negatives.","solutions":["Clamp/validate the vertex count to be non-negative before constructing the Graph","Trace where `vertex` originates and fix the upstream computation (e.g. guard empty inputs before `list.size() - 1`)","Pass an explicit positive literal once input parsing is confirmed correct"],"exampleFix":"// before\nGraph g = new BoruvkaAlgorithm.Graph(n - 1, edges);\n// after\nif (n < 1) throw new IllegalArgumentException(\"need >=1 vertex\");\nGraph g = new BoruvkaAlgorithm.Graph(n, edges);","handlingStrategy":"validation","validationCode":"if (vertex < 0) {\n    throw new IllegalArgumentException(\"vertex must be >= 0, got \" + vertex);\n}","typeGuard":null,"tryCatchPattern":"try {\n    new BoruvkaAlgorithm.Graph(v, edges);\n} catch (IllegalArgumentException e) {\n    // handle invalid vertex count\n}","preventionTips":["Validate external counts at the parse boundary before passing to Graph","Never derive vertex counts with unchecked subtraction that can go negative"],"tags":["graph","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}