{"record":{"id":"6fbf46e14961c081","repo":"tursodatabase/turso","slug":"columnindex-out-of-bound","errorCode":null,"errorMessage":"columnIndex out of bound","messagePattern":"columnIndex out of bound","errorType":"exception","errorClass":"java.sql.SQLException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/core/TursoResultSet.java","lineNumber":170,"sourceCode":"        return get(i + 1);\n      }\n    }\n\n    throw new SQLException(\"column name \" + columnName + \" not found\");\n  }\n\n  public Object get(int columnIndex) throws SQLException {\n    if (!this.isOpen()) {\n      throw new SQLException(\"ResultSet is not open\");\n    }\n\n    if (this.lastStepResult == null || this.lastStepResult.getResult() == null) {\n      throw new SQLException(\"ResultSet is null\");\n    }\n\n    final Object[] resultSet = this.lastStepResult.getResult();\n    if (columnIndex > resultSet.length || columnIndex < 0) {\n      throw new SQLException(\"columnIndex out of bound\");\n    }\n\n    return resultSet[columnIndex - 1];\n  }\n\n  public String[] getColumnNames() {\n    return this.columnNames;\n  }\n\n  public void setColumnNames(String[] columnNames) {\n    this.columnNames = columnNames;\n  }\n\n  @Override\n  public String toString() {\n    return (\"tursoResultSet{\"\n        + \"statement=\"\n        + statement","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/java/src/main/java/tech/turso/core/TursoResultSet.java#L152-L188","documentation":"get(int) validates the 1-based columnIndex against the current row array and throws when it exceeds resultSet.length or is negative. Note the guard tests columnIndex < 0, not < 1: index 0 passes validation and then crashes with a raw ArrayIndexOutOfBoundsException at resultSet[columnIndex - 1], i.e. resultSet[-1]. So this specific SQLException means 'too large or negative', while 0-based misuse crashes differently — the operative rule is that JDBC column indexes start at 1.","triggerScenarios":"Calling get(n + 1) where n is the column count; hardcoded indexes after the SELECT list changed; computed indexes going negative; passing get(0), which instead surfaces as ArrayIndexOutOfBoundsException.","commonSituations":"Converting code from 0-based APIs (JSON objects, CSV readers, arrays); SELECT lists edited without updating index constants; loops using <= instead of < against a 0-based length.","solutions":["Use 1-based indexes: the first column is get(1)","Derive the bound from rs.getColumnNames().length and validate before calling","Prefer get(String) with explicit SQL aliases when column order can shift"],"exampleFix":"// before\nObject id = rs.get(0);                              // ArrayIndexOutOfBoundsException\nObject name = rs.get(rs.getColumnNames().length + 1); // 'columnIndex out of bound'\n\n// after\nint cols = rs.getColumnNames().length; // 1-based: columns are get(1)..get(cols)\nfor (int i = 1; i <= cols; i++) {\n  Object v = rs.get(i);\n}","handlingStrategy":"validation","validationCode":"int columnCount = rs.getColumnNames().length;\nif (columnIndex < 1 || columnIndex > columnCount) {\n  throw new IllegalArgumentException(\"columnIndex must be 1..\" + columnCount);\n}\nObject v = rs.get(columnIndex);","typeGuard":null,"tryCatchPattern":"try {\n  return rs.get(columnIndex);\n} catch (SQLException | ArrayIndexOutOfBoundsException e) {\n  // note: get(0) crashes with ArrayIndexOutOfBoundsException, not this SQLException\n  throw new IllegalArgumentException(\"columnIndex \" + columnIndex + \" out of range\", e);\n}","preventionTips":["Remember JDBC column indexes are 1-based: get(0) is always wrong","Derive bounds from rs.getColumnNames().length instead of hardcoding","Prefer name-based access when the SELECT list can change"],"tags":["java","jdbc","result-set","index-out-of-bounds","off-by-one"],"backgroundTag":"index-out-of-bounds","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}