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);
5
  • How is your table model defined/initialized? Please add code for it Commented Sep 4, 2018 at 7: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); Commented Sep 4, 2018 at 9:06
  • public class UserModel { private int id; private String name,lname,gender; private int age; private Date dob; Commented Sep 4, 2018 at 9:07
  • Njena please edit your question instead of commenting with code. Commented Sep 4, 2018 at 9:31
  • For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example. Commented Sep 4, 2018 at 11:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.