Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6417303

TableSorter.java in Tutorial may produce null pointer exception

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 5.0
    • Fix Version/s: tbd
    • Component/s: client-libs
    • Labels:

      Description

      A DESCRIPTION OF THE PROBLEM :
      The Swing tutorial for using tables includes a section on sorting (http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting) which points to TableSorter.java. This class contains the following nested class:

          private class SortableHeaderRenderer implements TableCellRenderer {
              private TableCellRenderer tableCellRenderer;

              public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) {
                  this.tableCellRenderer = tableCellRenderer;
              }

              public Component getTableCellRendererComponent(JTable table,
                                                             Object value,
                                                             boolean isSelected,
                                                             boolean hasFocus,
                                                             int row,
                                                             int column) {
                  Component c = tableCellRenderer.getTableCellRendererComponent(table,
                          value, isSelected, hasFocus, row, column);
                  if (c instanceof JLabel) {
                      JLabel l = (JLabel) c;
                      l.setHorizontalTextPosition(JLabel.LEFT);
                      int modelColumn = table.convertColumnIndexToModel(column);
                      l.setIcon(getHeaderRendererIcon(modelColumn, l.getFont().getSize()));
                  }
                  return c;
              }
          }


      The problem is that in this statement,

          int modelColumn = table.convertColumnIndexToModel(column);

      table may be null, as specified in the TableCellRenderer interface documentation for the getTableCellRendererComponent() method:

          table - the JTable that is asking the renderer to draw; can be null

      This isn't just theoretical. Another example in the tutorial (http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/TableRenderDemo.java)
      does just this inside initColumnSizes():

          comp = headerRenderer.getTableCellRendererComponent(
                                       null, column.getHeaderValue(),
                                       false, false, 0, 0);
          headerWidth = comp.getPreferredSize().width;


      I realize Mustang will provide a TableRowSorter class to be used in place of code like TableSorter.java, but maybe this example can still be fixed.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code that doesn't produce a NullPointerException.
      ACTUAL -
      Code that produces a NullPointerException.

      URL OF FAULTY DOCUMENTATION :
      http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/TableSorter.java

        Attachments

          Activity

            People

            • Assignee:
              serb Sergey Bylokhov
              Reporter:
              rmandalasunw Ranjith Mandala (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: