0

I have six entries in my database/Jtable. When I click on JTable I display all selected data in text field except Date(dob). It shows me an error:

java.lang.ArrayIndexOutOfBoundsException: 5 >= 5

table = new JTable();
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        // TODO Auto-generated method stub
        //Write your code here 
        tabmod = table.getModel();
        int SelectedRowIndex = table.getSelectedRow();
        txtid.setText(tabmod.getValueAt(SelectedRowIndex, 0).toString());
        txtname.setText(tabmod.getValueAt(SelectedRowIndex, 1).toString());
        txtlname.setText(tabmod.getValueAt(SelectedRowIndex, 2).toString());
        String gender =tabmod.getValueAt(SelectedRowIndex, 3).toString();
        if(gender.equals("male")){
            rdbtnmale.setSelected(true);
        }else{
            rdbtnfemale.setSelected(true);
        }
        String dob = tabmod.getValueAt(SelectedRowIndex, 4).toString();
        txtage.setText(tabmod.getValueAt(SelectedRowIndex, 5).toString());//Here throw an error 5>=5 ArrayIndexOutOfBound
        //This line print the value eg id
        System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
        JOptionPane.showMessageDialog(null, "Click event active");

    }
});
tab_disp_user.setViewportView(table);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Njena
  • 191
  • 3
  • 11
  • How is your table model defined/initialized? Please add code for it – Piro Sep 04 '18 at 07:42
  • JScrollPane scrollPane = new JScrollPane(); scrollPane.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { String quer = "select * from userreg"; try { PreparedStatement pst = connection.prepareStatement(quer); ResultSet rs = pst.executeQuery(); table.setModel(DbUtils.resultSetToTableModel(rs)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); scrollPane.setBounds(387, 48, 537, 330); frame.getContentPane().add(scrollPane); – Njena Sep 04 '18 at 09:06
  • public class UserModel { private int id; private String name,lname,gender; private int age; private Date dob; – Njena Sep 04 '18 at 09:07
  • Njena please edit your question instead of commenting with code. – Piro Sep 04 '18 at 09:31
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 04 '18 at 11:34

1 Answers1

2

Your table has only five columns you are trying to get 6th column value for the selected row. Generally column index is counted (0 to n-1 ) where 0 is first column index and n-1 is nth column.