TheAlgorithms/Java · error · IllegalArgumentException
Node index out of bounds
Error message
Node index out of bounds
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/others/PageRank.java:121
* @param matrix the adjacency matrix (1-indexed)
*/
public void setAdjacencyMatrix(int[][] matrix) {
for (int i = 1; i <= nodeCount; i++) {
for (int j = 1; j <= nodeCount; j++) {
setEdge(i, j, matrix[i][j]);
}
}
}
/**
* Gets the PageRank value for a specific node
*
* @param node the node index (1-indexed)
* @return the PageRank value
*/
public double getPageRank(int node) {
if (node < 1 || node > nodeCount) {
throw new IllegalArgumentException("Node index out of bounds");
}
return pageRankValues[node];
}
/**
* Gets all PageRank values
*
* @return array of PageRank values (1-indexed)
*/
public double[] getAllPageRanks() {
return pageRankValues.clone();
}
/**
* Calculates PageRank using the default damping factor and iterations
*
* @param totalNodes the total number of nodes
* @return array of PageRank valuesView on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
for (int i = 0; i < nodeCount; i++) {
double pr = pageRank.getPageRank(i); // i==0 throws
}
// after
for (int i = 1; i <= nodeCount; i++) {
double pr = pageRank.getPageRank(i);
} Defensive patterns
Strategy: validation
Validate before calling
public static int toOneBased(int zeroBasedIndex, int nodeCount) {
if (zeroBasedIndex < 0 || zeroBasedIndex >= nodeCount) {
throw new IllegalArgumentException("index out of range");
}
return zeroBasedIndex + 1;
}
// usage:
double pr = pageRank.getPageRank(toOneBased(i, nodeCount)); Type guard
public static boolean isValidNodeQuery(int node, int nodeCount) {
return node >= 1 && node <= nodeCount;
} Try / catch
try {
value = pageRank.getPageRank(node);
} catch (IllegalArgumentException e) {
// node is 1-indexed; if you passed a 0-based index, add 1 and retry
throw e;
} Prevention
- 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.
When it happens
Trigger: Calling getPageRank(node) with node < 1, node > nodeCount, or node == 0 (a common 0-based-index mistake).
Common situations: 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.
Related errors
- Position must be between 0 and {array.length}
- Position must be between 0 and + (array.length - 1)
- Number of nodes must be between 1 and + MAX_NODES
- Total nodes must be between 1 and + MAX_NODES
- Damping factor must be between 0 and 1
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/77b92546fc6f15f4.
Report an issue: GitHub.