Click here to Skip to main content
15,868,047 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
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8 at java.base/java.util.Vector.elementData(Vector.java:731) at java.base/java.util.Vector.elementAt(Vector.java:469) at java.desktop/javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:660) at com.mycompany.hotel_management_system.Final_Frame.jButton3ActionPerformed(Final_Frame.java:870) at com.mycompany.hotel_management_system.Final_Frame$6.actionPerformed(Final_Frame.java:625) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6616) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398) at java.desktop/java.awt.Component.processEvent(Component.java:6381) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)



And this is the method where the exception occurs.

Java
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) {
                model.removeRow(mainTable.getSelectedRow());
                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);
                    pst.executeUpdate();
                    
                    
                } 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.
Posted
Updated 13-Sep-22 5: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:
Java
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
 
Comments
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)

  Print Answers RSS


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