{"record":{"id":"77b92546fc6f15f4","repo":"TheAlgorithms/Java","slug":"node-index-out-of-bounds","errorCode":null,"errorMessage":"Node index out of bounds","messagePattern":"Node index out of bounds","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/PageRank.java","lineNumber":121,"sourceCode":"     * @param matrix the adjacency matrix (1-indexed)\n     */\n    public void setAdjacencyMatrix(int[][] matrix) {\n        for (int i = 1; i <= nodeCount; i++) {\n            for (int j = 1; j <= nodeCount; j++) {\n                setEdge(i, j, matrix[i][j]);\n            }\n        }\n    }\n\n    /**\n     * Gets the PageRank value for a specific node\n     *\n     * @param node the node index (1-indexed)\n     * @return the PageRank value\n     */\n    public double getPageRank(int node) {\n        if (node < 1 || node > nodeCount) {\n            throw new IllegalArgumentException(\"Node index out of bounds\");\n        }\n        return pageRankValues[node];\n    }\n\n    /**\n     * Gets all PageRank values\n     *\n     * @return array of PageRank values (1-indexed)\n     */\n    public double[] getAllPageRanks() {\n        return pageRankValues.clone();\n    }\n\n    /**\n     * Calculates PageRank using the default damping factor and iterations\n     *\n     * @param totalNodes the total number of nodes\n     * @return array of PageRank values","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/PageRank.java#L103-L139","documentation":"Thrown by PageRank.getPageRank when the requested node is outside [1, nodeCount]. Nodes are 1-indexed in this API: node 1 is the first and node nodeCount is the last. Requesting node 0, a negative node, or a node greater than nodeCount is rejected before indexing the internal PageRank array.","triggerScenarios":"Calling getPageRank(node) with node < 1, node > nodeCount, or node == 0 (a common 0-based-index mistake).","commonSituations":"Treating the API as 0-indexed when it is 1-indexed; iterating 0..nodeCount-1 instead of 1..nodeCount; off-by-one after construction with a smaller nodeCount than expected.","solutions":["Use 1-based indices: iterate node from 1 to nodeCount inclusive.","If you hold 0-based indices, add 1 before calling getPageRank.","Verify nodeCount matches the graph size you expect before querying.","Clamp/validate external indices against [1, nodeCount] at the boundary."],"exampleFix":"// before\nfor (int i = 0; i < nodeCount; i++) {\n    double pr = pageRank.getPageRank(i); // i==0 throws\n}\n\n// after\nfor (int i = 1; i <= nodeCount; i++) {\n    double pr = pageRank.getPageRank(i);\n}","handlingStrategy":"validation","validationCode":"public static int toOneBased(int zeroBasedIndex, int nodeCount) {\n    if (zeroBasedIndex < 0 || zeroBasedIndex >= nodeCount) {\n        throw new IllegalArgumentException(\"index out of range\");\n    }\n    return zeroBasedIndex + 1;\n}\n// usage:\ndouble pr = pageRank.getPageRank(toOneBased(i, nodeCount));","typeGuard":"public static boolean isValidNodeQuery(int node, int nodeCount) {\n    return node >= 1 && node <= nodeCount;\n}","tryCatchPattern":"try {\n    value = pageRank.getPageRank(node);\n} catch (IllegalArgumentException e) {\n    // node is 1-indexed; if you passed a 0-based index, add 1 and retry\n    throw e;\n}","preventionTips":["This API is 1-indexed: iterate 1..nodeCount inclusive.","Convert 0-based indices by adding 1 before calling.","Confirm nodeCount matches the graph before querying."],"tags":["validation","pagerank","indexing","input-validation","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}