Question

in this code i am getting data from data base that data represent in rows and column but i am not getting fifth column data from data base .i am entering that data through keyboard when i perform action on submit button at that time i want to delete those record who can fill up fifth column. if record is not entered in fifth column that row can not be deleted .but problem is that some record deleted and some record are not deleted means i am not getting what i want and some show array index out of bounds exceptions are generated

public class Add_EmployeeAttendance {

            DBConnection connect = new DBConnection();
        static ArrayList emp_code,No_Of_Working_Days,Wages,Professional_Tax,Provisional_Tax,Eisc,Canteen,Gross_Salary1;
     public static final String[] COLUMN_NAMES = { "CompanyName", "Department",
                    "Designation", "Emp_Code", "Emp_Name", "working_days " };
        public static String Company_name,Department,Designation;
            private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
            public static int a = 0;
            private JTable table = new JTable(model);
            public JComboBox <String>b, b1, b2;
            public static String strres = null, getdesignation_id = null,
                    strdept_id = null;
            private JPanel mainPanel = new JPanel(new BorderLayout());
            public class TableDataDelete implements ActionListener
            {
                public void actionPerformed(ActionEvent ee) {
                    int count=0;
                    if(table.getCellEditor() != null) 
                    {
                        int count1=0;
                        table.getCellEditor().stopCellEditing();
                    int b = table.getRowCount();
                    System.out.println("row count" + b);

                    for (int i = 0; i <b; i++) {
                    try
                        {
                        if(table.getCellEditor(i, 5) != null) 
                            {
                        System.out.println("hit count is "+count1);
                        count1++;
                        String str1 = (String) table.getModel().getValueAt(i, 5);
                    System.out.println("second value"+model.getValueAt(i,5));
                        if (!(str1 == null)) {
                            ((DefaultTableModel)table.getModel()).removeRow(i);
                            table.repaint();
                            System.out.println("remove row no is"+i);
                        }
                        }
                    }
                        catch(ArrayIndexOutOfBoundsException e)
                        {
                            System.out.println("array index out of bound exception"+e);
                        }

                    }
                }

                }
            }
        public Add_EmployeeAttendance() {

                JPanel buttonPanel = new JPanel();
                JPanel buttonPanel1 = new JPanel();
                b = new JComboBox();
                b.addItem("Please select Company");
                connect.getConnection();
                Statement st = null;
                try {
                    st = connect.conn.createStatement();
                    ResultSet rs1 = st
                            .executeQuery("select company_name from company_details");
                    while (rs1.next()) {
                        b.addItem(rs1.getString(1));
                            System.out.println(rs1.getString(1));
                    }
                } catch (SQLException e) {

                    e.printStackTrace();
                }
        table.setFocusable(true);
                b.addItem("Please Select Company");
                buttonPanel.add(b);
                b1 = new JComboBox();
                b1.addItem("Please Select Department");
                buttonPanel.add(b1);
                b2 = new JComboBox();
                b2.addItem("Please Select Designation");
                buttonPanel.add(b2);
                b.addItemListener(new companyEvents());
                b1.addItemListener(new Designation());
                b2.addItemListener(new getDesignation_id());
                JButton addDataButton = new JButton("Show Data");
                buttonPanel.add(addDataButton);
                JButton submit = new JButton("View Salary ");
                buttonPanel1.add(submit);
                JButton submit1=new JButton("submit");
                buttonPanel1.add(submit1);
                submit.addActionListener(new TableDataRetriving());
                submit1.addActionListener(new TableDataDelete());
                addDataButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            model.setRowCount(0);
                            addDataActionPerformed();

                        } catch (SQLException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                });
                mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
                mainPanel.add(buttonPanel, BorderLayout.PAGE_START);
                mainPanel.add(buttonPanel1, BorderLayout.PAGE_END);
                new DefaultListCellRenderer();
                emp_code=new ArrayList();
                No_Of_Working_Days=new ArrayList();
                Wages=new ArrayList();
                Professional_Tax=new ArrayList();
                Provisional_Tax=new ArrayList();
                Eisc=new ArrayList();
               Canteen=new ArrayList();
               Gross_Salary1=new ArrayList();
               }
        private void addDataActionPerformed() throws SQLException {
                connect.getConnection();
                  Object[] row = new Object[COLUMN_NAMES.length];
                    PreparedStatement stmt1 = connect.conn
                        .prepareStatement("select emp_id,employee_code from employee_master where company_id=? and dept_id=? and designation_id=?");
                stmt1.setString(1, strres);
                stmt1.setString(2, strdept_id);
                stmt1.setString(3, getdesignation_id);
                ResultSet resemp = stmt1.executeQuery();
                String str1 = null, str2 = null, str3 = null;
                while (resemp.next()) {

        //          for (int j = 0; j < row.length; j++) {
                        PreparedStatement stmt = connect.conn
                                .prepareStatement("select * from personal_details where emp_id=? ");
                        stmt.setString(1, resemp.getString(1));
                        System.out.println("empid is" + resemp.getString(1));
                        ResultSet res1 = stmt.executeQuery();
                        while (res1.next()) {
                            str1 = res1.getString(3);
                            str2 = res1.getString(4);
                            str3 = res1.getString(5);
                        }
                                    row[0] = String.valueOf(b.getSelectedItem());
                        row[1] = String.valueOf(b1.getSelectedItem());
                        row[2] = String.valueOf(b2.getSelectedItem());
                            row[3] = resemp.getString(2);
                                    row[4] = str1 + " " + str2 + " " + str3;
                        table.setRowHeight(30);
                        model.addRow(row);
                }

                a = table.getModel().getRowCount();
            }
        public JComponent getComponent() {
                return mainPanel;
            }
            public static void main(String[] args) {

                java.awt.EventQueue.invokeLater(new Runnable() {

                    public void run() {

                        JFrame frame = new JFrame("Employee Details");

                        frame.getContentPane().add(
                                new Add_EmployeeAttendance().getComponent());
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.setSize(1200, 650);
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
        }
                });
            }
Was it helpful?

Solution

Based on this stack trace:

array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 6 >= 6
array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 7 >= 6
array index out of bound exceptionjava.lang.ArrayIndexOutOfBoundsException: 8 >= 6 

Your problem is here:

int b = table.getRowCount();
for (int i = 0; i < b; i++) {
    ...
    if (!(str1 == null)) {
        ((DefaultTableModel)table.getModel()).removeRow(i);
        ...
    }
    ...
}

If you remove a row from the table model within the for loop then b won't be longer equals to table.getRowCount() but greater instead, causing an ArrayIndexOutOfBoundsException.

I would replace it by this:

DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = model.getRowCount() - 1; i >= 0; i--) { // ends condition is updated dinamically
    ...
    if (str1 != null) {
        model.removeRow(i);            
        ...
    }
    ...
}

Side note

There's no need to repaint the table when model data has changed (insert/update/delete). The model will notify the view in such event and this one will be updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top