You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL表越界异常-1及点击表项触发数组越界异常问题咨询

Hey there, let’s tackle these two linked issues you’re dealing with—both boil down to mishandling index values, either in your database query layer or the UI interaction logic. Let’s break each one down with fixes and examples:

1. MySQL Table Out-of-Bounds Exception (-1)

First up, that Exception -1 from MySQL is almost always tied to invalid index values being passed in your database operations. Here’s what to check:

  • JDBC Column Index Mistake: JDBC uses 1-based indexing for ResultSet columns. If you’re accidentally passing -1 to methods like getString(-1) or getInt(-1), that’ll throw this error. Using column names instead of indexes is safer and avoids this entirely.
  • Invalid Pagination Offset: If your code calculates a pagination OFFSET that ends up negative (e.g., miscalculating page numbers where page 0 becomes offset -10), MySQL will reject the query with an out-of-bounds error.
  • Outdated JDBC Driver: Older versions of the MySQL JDBC driver had edge cases where certain operations could incorrectly return -1 as an error code. Updating to the latest stable driver often resolves this.
  • Corrupted Table Metadata: Rarely, if a table’s metadata is corrupted, you might get index-related errors. Running ANALYZE TABLE your_table_name; can help refresh metadata.

Example Fix for JDBC Index Issue

Bad code that causes the error:

// ❌ Using -1 as column index (invalid in JDBC)
String userEmail = resultSet.getString(-1);

Fixed code:

// ✅ Use column name for safety, or valid 1-based index
String userEmail = resultSet.getString("email");
// Or, if you prefer indexes:
String userEmail = resultSet.getString(3); // Assuming email is the 3rd column

2. UI Table Click Triggers java.lang.ArrayIndexOutOfBoundsException: -1

Even though your table displays correctly, clicking it throws this error because your click handler is trying to use an index value of -1 to access an array/collection. -1 usually means no row was actually selected when the click happened—here’s how to fix it:

  • Add a Guard Clause for -1 Index: Always check if the selected row index is -1 before trying to use it. This handles cases where the user clicks empty space in the table instead of a row.
  • Verify Index Mapping: Make sure the index you’re getting from the table matches the index of your backing data list. For example, if your table uses a filtered view, the table’s row index might not align with the original list’s index—you’ll need to convert it first.
  • Debug the Index Value: Add a print statement or debugger breakpoint to check what index is being returned when the click happens. This will confirm if -1 is coming from the table’s selection method (like getSelectedRow() in Swing) or somewhere else.

Example Fix for Swing Table Click Handler

Bad code that triggers the exception:

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        int selectedRow = table.getSelectedRow();
        // ❌ No check for -1, throws error if no row is selected
        User selectedUser = userList.get(selectedRow);
        // Rest of the logic...
    }
});

Fixed code with guard clause:

table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        int selectedRow = table.getSelectedRow();
        // ✅ Guard against -1 index
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(null, "Please select a row first!");
            return;
        }
        // If using a TableModel with filtered data, convert to model index first:
        int modelRow = table.convertRowIndexToModel(selectedRow);
        User selectedUser = userList.get(modelRow);
        // Rest of the logic...
    }
});

Hope these steps help you fix both issues smoothly! If you run into specific edge cases while debugging, feel free to share more details.

内容的提问来源于stack exchange,提问作者Alisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:41:17