2

I have created a JTable with the last (6th) column being a "Select" column which is having checkboxes. The checkboxes are added by overriding the getColumnClass method to return Boolean.class.

    DefaultTableModel dtm = new DefaultTableModel(data, header){
            @Override
            public Class<?> getColumnClass(int col) {
                if(col == 5){
                    return Boolean.class;
                }
                return super.getColumnClass(col);
            }
        };

Now I have to select some rows using these checkboxes and add some values from those selected rows to the database. This is the code I used.

DefaultTableModel model = (DefaultTableModel) tblInvoiceList.getModel();
for(int i=0; i<model.getRowCount();i++) {
    if ((Boolean)model.getValueAt(i, 5))
        paydao.updateInvoiceWithPayment(model.getValueAt(i, 0).toString());
}

When I loop through, the checked checkboxes are returning true. The unchecked ones are throwing NullPointerException. The exception occurs in the (Boolean)model.getValueAt(i, 5) part. How to make the unchecked ones return false??

FireDrakon
  • 275
  • 2
  • 6
  • 19

1 Answers1

5

The default table cell editor may initially accept null, but your loop does not. As shown here and here, the model values rendered as unchecked should be set to Boolean.FALSE or false explicitly when you fetch the table's data.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045