Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem always occurs when I tried to click and then delete a row in my GUI using the Delete JButton that I created.

Then this is the exception I always get whenever I try to delete a row from my GUI. And as a result of the error, nothing happened in my database even though I already used the delete option in my GUI. I have a total of 14 columns in my table(and also in my database) so I'm wondering where did the length 8 come from when I'm pretty sure I didn't make any table or any loop or array that has a length of 8.

Exception that I got

java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8 at java.base/java.util.Vector.elementData( at java.base/java.util.Vector.elementAt( at java.desktop/javax.swing.table.DefaultTableModel.getValueAt( at com.mycompany.hotel_management_system.Final_Frame.jButton3ActionPerformed( at com.mycompany.hotel_management_system.Final_Frame$6.actionPerformed( at java.desktop/javax.swing.AbstractButton.fireActionPerformed( at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed( at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed( at java.desktop/javax.swing.DefaultButtonModel.setPressed( at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased( at java.desktop/java.awt.AWTEventMulticaster.mouseReleased( at java.desktop/java.awt.Component.processMouseEvent( at java.desktop/javax.swing.JComponent.processMouseEvent( at java.desktop/java.awt.Component.processEvent( at java.desktop/java.awt.Container.processEvent( at java.desktop/java.awt.Component.dispatchEventImpl( at java.desktop/java.awt.Container.dispatchEventImpl( at java.desktop/java.awt.Component.dispatchEvent( at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent( at java.desktop/java.awt.LightweightDispatcher.processMouseEvent( at java.desktop/java.awt.LightweightDispatcher.dispatchEvent( at java.desktop/java.awt.Container.dispatchEventImpl( at java.desktop/java.awt.Window.dispatchEventImpl( at java.desktop/java.awt.Component.dispatchEvent( at java.desktop/java.awt.EventQueue.dispatchEventImpl( at java.desktop/java.awt.EventQueue$ at java.desktop/java.awt.EventQueue$ at java.base/ at java.base/$JavaSecurityAccessImpl.doIntersectionPrivilege( at java.base/$JavaSecurityAccessImpl.doIntersectionPrivilege( at java.desktop/java.awt.EventQueue$ at java.desktop/java.awt.EventQueue$ at java.base/ at java.base/$JavaSecurityAccessImpl.doIntersectionPrivilege( at java.desktop/java.awt.EventQueue.dispatchEvent( at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters( at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter( at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy( at java.desktop/java.awt.EventDispatchThread.pumpEvents( at java.desktop/java.awt.EventDispatchThread.pumpEvents( at java.desktop/

And this is the method where the exception occurs.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DefaultTableModel model = (DefaultTableModel) mainTable.getModel();
    if (mainTable.getSelectedRow() != -1) {
        if (mainTable.getRowCount() != 0) {
            if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this row?", "Hotel Management System", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null, "Hotel Booking Update Confirmed", "Hotel Mangement System", JOptionPane.OK_OPTION);
                //Deleting in the database
                try {
                    //connecting to the database
                    connection = connector.getConnection();
                    pst = connection.prepareStatement("DELETE FROM hotel_management_system_main WHERE reference_number = ?");
                    //identifying the selected row
                    int selectedRows = mainTable.getSelectedRow();
                    //getting the reference number of the selected row then using it to delete the row it belongs in the database
                    int id1 = Integer.parseInt(model.getValueAt(selectedRows, 12).toString());
                    pst.setInt(1, id1);
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(null, e);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex);
    else {
        JOptionPane.showMessageDialog(null, "Please select a row to delete", "Hotel Management System", JOptionPane.OK_OPTION);

Btw mainTable is the variable name of the JTable that I used and pst for my prepared statement.

What I have tried:

I tried doing it over again and rechecking the proper syntax, but I still can't get it.
Updated 13-Sep-22 6:54am

1 solution

java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8 at 

The error is that you are trying to use an index value of -1. It is a reasonable guess that the problem is caused by the line:
int selectedRows = mainTable.getSelectedRow();

So if getSelectedRow does not find a row with the selected status, it will return -1. So you should be able to check that, both visually, and within your code.

If that is not the case then you need to use the debugger to get exact details.
Share this answer
Ralph Daniel Honra 14-Sep-22 0:21am    
Thank you very much for pointing up the issue; your remark really helped!
So, the issue is that I removed the row in my GUI first, making it impossible for the code that follows it (the one about removing it in the database) to determine which row I picked because I had already deleted it. Again, many thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900