apache/shardingsphere · error · ColumnIndexOutOfRangeException

20

20

Error message

Column index '%d' is out of range.

What it means

ColumnIndexOutOfRangeException (code 20) thrown by DatabaseMetaDataResultSet.checkColumnIndex when columnIndex < 1 or > resultSetMetaData.getColumnCount(). It is a plain bounds check on the 1-based index into the metadata result set's columns.

Source

Thrown at jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java:428

    @Override
    public void setFetchSize(final int rows) throws SQLException {
        resultSet.setFetchSize(rows);
    }
    
    @Override
    public boolean isClosed() {
        return closed;
    }
    
    private void checkClosed() throws SQLException {
        if (closed) {
            throw new ResultSetClosedException().toSQLException();
        }
    }
    
    private void checkColumnIndex(final int columnIndex) throws SQLException {
        if (columnIndex < 1 || columnIndex > resultSetMetaData.getColumnCount()) {
            throw new ColumnIndexOutOfRangeException(columnIndex).toSQLException();
        }
    }
    
    @EqualsAndHashCode
    private static final class DatabaseMetaDataObject {
        
        private final List<Object> objects;
        
        private DatabaseMetaDataObject(final int columnCount) {
            objects = new ArrayList<>(columnCount);
        }
        
        public void addObject(final Object object) {
            objects.add(object);
        }
        
        public Object getObject(final int index) {
            return objects.get(index - 1);

View on GitHub (pinned to e952770a21)

Solutions

  1. Use 1-based indices: iterate from 1 to rs.getMetaData().getColumnCount() inclusive.
  2. Prefer column labels over indices for metadata result sets.
  3. Print the column count first when unsure of the layout.

Example fix

// before
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { rs.getString(i); }
// after
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { rs.getString(i); }
Defensive patterns

Strategy: validation

Validate before calling

int cols = rs.getMetaData().getColumnCount();
if (index < 1 || index > cols) throw new IndexOutOfBoundsException("use 1.." + cols);

Prevention

When it happens

Trigger: Calling getObject(int)/getXxx(int) on a DatabaseMetaData ResultSet with index 0, a negative index, or an index beyond the number of columns that metadata method returns.

Common situations: Looping with 0-based indices over 1-based JDBC columns; assuming a metadata result set has more columns than it does (driver-specific extras); ported code from another driver with a wider metadata layout.

Related errors


AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14). Data as JSON: /api/errors/33591f83086241f7. Report an issue: GitHub.