JDK-4247812 : JTree doesn't handle TreeModels with null root node
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.8,1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1999-06-18
  • Updated: 2000-02-03
  • Resolved: 2000-02-03
Related Reports
Duplicate :  
Description

Name: skT88420			Date: 06/18/99


The javax.swing.tree.TreeModel interface comments indicate 
that null is a valid result for the getRoot() method -- it 
means that the tree contains no nodes.

However, the setModel(TreeModel) method of javax.swing.JTree 
assumes that the result of getRoot() is always non-null and 
passes the null to the single-arg constructor of 
javax.swing.tree.TreePath.  The TreePath constructor throws 
a NullPointerException, and setModel fails.

Personally, I'd like to see the getDefaultTreeModel method 
return a truly empty Tree, with a null root node.  The "colors" 
and "sports" tree used now is not appropriate for any real 
application, so the model must alway be set explicitly before 
the tree is displayed...
(Review ID: 84501) 
======================================================================

Name: krT82822			Date: 08/27/99


The documentation for TreeModel explicitly states (in the description of getRoot() ) that a TreeModel may return null to indicate that a tree has no nodes.

However, the implementation of JTree raises an IllegalArgumentException if TreeModel.getRoot() actually returns null.

The problem seems to be in javax.swing.tree.VariableHeightLayoutCache.rebuild(); here is a backtrace of the exception:

Exception in thread "main" java.lang.IllegalArgumentException: path in TreePath
must be non null.
        at javax.swing.tree.TreePath.<init>(TreePath.java:71)
        at javax.swing.tree.VariableHeightLayoutCache.rebuild(VariableHeightLayo
utCache.java:663)
        at javax.swing.tree.VariableHeightLayoutCache.setModel(VariableHeightLay
outCache.java:96)
        at javax.swing.plaf.basic.BasicTreeUI.setModel(BasicTreeUI.java:302)
        at javax.swing.plaf.basic.BasicTreeUI$PropertyChangeHandler.propertyChan
ge(BasicTreeUI.java:2596)
        at javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(Swing
PropertyChangeSupport.java:154)
        at javax.swing.JComponent.firePropertyChange(JComponent.java:3023)
        at javax.swing.JTree.setModel(JTree.java:642)
        at javax.swing.JTree.<init>(JTree.java:460)
(Review ID: 94535)
======================================================================

Comments
WORK AROUND Name: skT88420 Date: 06/18/99 Return something like an empty String even if your TreeModel is really empty. ====================================================================== Name: krT82822 Date: 08/27/99 When creating a JTree where the root node is not known at tree creation time, create a psuedo-root node with no children, and call JTree.setRootVisible(false) so that it will not be displayed. (Review ID: 94535) ======================================================================
11-06-2004

EVALUATION This is a dup of 4264897, refer to it for more info. scott.violet@eng 2000-02-03
03-02-2000