JDK-6459915 : DefaultTableCellRenderer.getTableCellRendererComponent() throws unexpected NPE
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2006-08-14
  • Updated: 2010-04-02
  • Resolved: 2006-11-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7Resolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
OS- and configuration-independent

A DESCRIPTION OF THE PROBLEM :
According to the TableCellRenderer interface spec, null is a legal table argument for getTableCellRendererComponent().
javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(), however, throws an NPE when given a null table.

As a result, TableColumn.sizeWidthToFit() fails with an NPE if a DefaultTableCellRenderer has been set as header renderer.
This is because sizeWidthToFit() asks the column header renderer for the renderer component to get its preferred width, but, not having access to the enclosing table, passes a null value for the table argument.

This seems to be a duplicate of bug 4117059 (of 1998!), which has reportedly been fixed long ago, but was apparently re-introduced at some point.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached code snippet

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Silent completion of the main()  method
ACTUAL -
See attached NPE stacktrace

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NullPointerException
	at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:148)
	at javax.swing.table.TableColumn.sizeWidthToFit(TableColumn.java:650)
	at SizeWidthToFitSnippet.main(SizeWidthToFitSnippet.java:13)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class SizeWidthToFitSnippet {

	public static void main(String[] args) {
		JTable table = new JTable(1, 1);
		TableColumn column = table.getColumnModel().getColumn(0);
		column.setHeaderRenderer(new DefaultTableCellRenderer());
		column.sizeWidthToFit();
	}

}

---------- END SOURCE ----------

Comments
EVALUATION Duplicate of 4765279.
14-11-2006

EVALUATION This could be a DUP of 4765279 (thanks to jdk.dev.java.net member leouser)
14-11-2006

EVALUATION Agreed. Should be fixed. Not a regression - fails in 1.4.2_08, 1.5.0_07, 6.0.
14-08-2006

EVALUATION It appears after the huge fix for the 4468566 bug (Swing DnD should not use selection to show drop location), see the getTableCellRendererComponent() in webrev here: http://sa.sfbay.sun.com/projects/swing_data/mustang/4468566/src/share/classes/javax/swing/table/DefaultTableCellRenderer.java.sdiff.html But the problem was earlier, see the 152 line of the 1.42 version of DefaultTableCellRenderer.java: if the isSelected is true and the table is null, also was the NPE. This code was since the very beginning (1.2 file version).
14-08-2006