Review rquest for 7055065, NPE from JTable when quit editing with empty value in number culumns (original) (raw)

Jonathan Lu luchsh at linux.vnet.ibm.com
Thu Apr 12 11:37:12 UTC 2012


Hello swing-dev,

I've got a patch for bug 7055065, can anybody please help to take a look? http://cr.openjdk.java.net/~luchsh/7055065/

And also a simple test case for this issue here.

/*

/*

import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; import java.awt.Dimension; import java.awt.GridLayout;

public class TableDemo extends JPanel {

 public TableDemo() {
     super(new GridLayout(1, 0));

     final String[] columnNames = { "String", "Number" };
     final Object[][] data = { { "aaaa", new Integer(5) },
             { "bbbb", new Integer(3) }, { "cccc", new Integer(2) },
             { "dddd", new Integer(20) }, { "eeee", new Integer(10) } };
     final JTable table = new JTable(data, columnNames);

     table.setPreferredScrollableViewportSize(new Dimension(500, 400));
     table.setFillsViewportHeight(true);
     TableModel dataModel = new AbstractTableModel() {

         public int getColumnCount() {
             return columnNames.length;
         }
         public int getRowCount() {
             return data.length;
         }
         public Object getValueAt(int row, int col) {
             return data[row][col];
         }
         public String getColumnName(int column) {
             return columnNames[column];
         }
         public Class<?> getColumnClass(int c) {
             return getValueAt(0, c).getClass();
         }
         public boolean isCellEditable(int row, int col) {
             return col != 5;
         }
         public void setValueAt(Object aValue, int row, int column) {
             data[row][column] = aValue;
         }
     };
     table.setModel(dataModel);
     TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
             dataModel);
     table.setRowSorter(sorter);

     JScrollPane scrollPane = new JScrollPane(table);
     add(scrollPane);
 }

 public static void main(String[] args) throws Exception {
     javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
         public void run() {
             JFrame frame = new JFrame("SimpleTableDemo");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             TableDemo newContentPane = new TableDemo();
             newContentPane.setOpaque(true);
             frame.setContentPane(newContentPane);

             frame.pack();
             frame.setVisible(true);
         }
     });
 }

}

To reproduce the problem, please click on one cell from the "Number" column, delete all the content but do not press enter to quit editing status, then click the column title of "Number" column to sort, NPE will be thrown.

The cause of this problem is when trying to quit editing with empty content of a cell and also try to accept the partially edited value using stopCellEditing(), following two actions will be taken. 1, the cellEditor of current table will be set to null. 2, the value type of this column does not have a constructor to accept "Object[]" parameter, false will be returned. Then swing will try to cancel edition without accepting the partially edited value using cancelCellEditing() which will get null for the value of cellEditor thus lead to this NPE.

The patch is trying to return earlier before the type compatibility check of partially edited values when empty cell values encountered.

Cheers!



More information about the swing-dev mailing list