JDK-6505523 : NullPointerException in BasicTreeUI when a node is removed by expansion listener
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-12-18
  • Updated: 2014-03-18
  • Resolved: 2012-01-25
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 6 JDK 7 JDK 8
6u38Fixed 7u4Fixed 8 b23Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
A JTree has expansion listener, which removes or replaces a node when it's expanded. This JTree has also dragEnabled property set to true.

When a node is expanded, a NullPointerException is thrown from the BasicTreeUI$Handler.isActualPath method (going up from mouseReleased()).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the attached source code. Expand the "expand me" node. See the stderr.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Node should disappear, no exceptions.
ACTUAL -
NPE (see below)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at javax.swing.plaf.basic.BasicTreeUI$Handler.isActualPath(BasicTreeUI.java:3520)
	at javax.swing.plaf.basic.BasicTreeUI$Handler.mouseReleasedDND(BasicTreeUI.java:3688)
	at javax.swing.plaf.basic.BasicTreeUI$Handler.mouseReleased(BasicTreeUI.java:3661)
	at java.awt.Component.processMouseEvent(Component.java:6038)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
	at java.awt.Component.processEvent(Component.java:5803)
	at java.awt.Container.processEvent(Container.java:2058)
	at java.awt.Component.dispatchEventImpl(Component.java:4410)
	at java.awt.Container.dispatchEventImpl(Container.java:2116)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
	at java.awt.Container.dispatchEventImpl(Container.java:2102)
	at java.awt.Window.dispatchEventImpl(Window.java:2429)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.tree.*;

public class TreeExpansionProblem implements Runnable {
  public static void main(String[] args) throws IllegalAccessException, UnsupportedLookAndFeelException, InstantiationException, ClassNotFoundException {
    SwingUtilities.invokeLater(new TreeExpansionProblem());
  }

  public void run() {
    final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6");
    final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr");
    problematic.add(new DefaultMutableTreeNode("some content"));
    root.add(new DefaultMutableTreeNode("irrelevant..."));
    root.add(problematic);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);
    tree.expandRow(0);
    tree.collapseRow(2);

    // this is critical - without dragEnabled everything works
    tree.setDragEnabled(true);

    tree.addTreeExpansionListener(new TreeExpansionListener() {
      public void treeExpanded(TreeExpansionEvent event) {
        TreeNode parent = problematic.getParent();
        if (parent instanceof DefaultMutableTreeNode) {
          model.removeNodeFromParent(problematic);
        }
      }

      public void treeCollapsed(TreeExpansionEvent event) {
      }
    });

    JFrame frame = new JFrame("JTree Problem");
    frame.add(new JScrollPane(tree));
    frame.setSize(500, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.show();
  }

}

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

Release Regression From : 5.0u7
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION [SQE] Risk: low To test the fix run the test/javax/swing/JTree/6505523/bug6505523.java test. [SQE]
10-01-2012

EVALUATION isActualPath() method should be fixed to check the bounds for null and ensureRowsAreVisible() should be fixed the same way as for WindowsTreeUI (4909150)
16-06-2011

EVALUATION I finally made it, the exception is thrown only when you click once at the beginning of the node, with double-click the node just dissapear.
16-06-2011

EVALUATION I checked JDK 6u23 and u28 - the bug is not reproducible.
16-06-2011