0

I have a method wherein it will display the value of a filtered JTable to JTextField. I plan to call this method in an action event. I tried to search for a fix by searching the first two lines of the error but all I can find so far involves only either the first line or the second line. I've also tried changing the value in the iteration (setting i=1 and i--) but the error is still the same.

This is the method that I've created:

public void CheckOut() {
    if (txtCheckOutRoomNo.getText().equals("") || txtCheckOutCOUT.getText().equals("")) {
        JOptionPane.showMessageDialog(this, "Please completely fill up the information!");
    }
    else {
        String[] checkin = {null, null, cmbCheckInType.getSelectedItem().toString(), txtCheckInName.getText(), txtCheckInCIN.getText(), txtCheckOutCOUT.getText()};
        DefaultTableModel checkinModel = (DefaultTableModel)tableGuestsProfile.getModel();        
        TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel> (checkinModel);
        tableGuestsProfile.setRowSorter(tr);
        tr.setRowFilter(RowFilter.regexFilter(txtCheckOutRoomNo.getText().trim()));
        for (int i = 0; i <= tableGuestsProfile.getRowCount(); i++) {
            tableGuestsProfile.convertRowIndexToModel(i);
            txtCheckOutName.setText(tableGuestsProfile.getModel().getValueAt(tableGuestsProfile.convertRowIndexToModel(i), 3).toString());
            txtCheckOutType.setText(tableGuestsProfile.getModel().getValueAt(tableGuestsProfile.convertRowIndexToModel(i), 2).toString());
        }
        JOptionPane.showMessageDialog(this, "Checked-out successfully!");
    }
}

It's working fine and building however, the message dialog is not showing up and I kept on receiving this error. This is the first three lines of the error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at java.desktop/javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:508)
at java.desktop/javax.swing.JTable.convertRowIndexToModel(JTable.java:2629)

I'm honestly bummed out as I have little knowledge about programming so I'm hoping for your kind assistance. Thank you.

Robin
  • 1
  • The line `for (int i = 0; i <= tableGuestsProfile.getRowCount(); i++)` needs to be `for (int i = 0; i < tableGuestsProfile.getRowCount(); i++)`. Remember that arrays and likewise in programming are 0 indexed and the maximum index of the `rowCount` isn't equal to its size, therefore you get that error. – Alias Cartellano Jan 18 '22 at 18:41

0 Answers0