JTree.updateUI() during TreeSelectionEvent.valueChanged() causes Null Pointer

Bug链接:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5089562

 

ERROR MESSAGES/STACK TRACES THAT OCCUR :

java.lang.NullPointerException
        at javax.swing.plaf.basic.BasicTreeUI.completeEditing(BasicTreeUI.java:1
880)
        at javax.swing.plaf.basic.BasicTreeUI$TreeSelectionHandler.valueChanged(
BasicTreeUI.java:2496)
        at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTr
eeSelectionModel.java:629)
        at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTr
eeSelectionModel.java:1076)
        at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultT
reeSelectionModel.java:287)
        at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTr
eeSelectionModel.java:170)
        at javax.swing.JTree.setSelectionPath(JTree.java:1168)
        at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.jav
a:2192)
        at javax.swing.plaf.basic.BasicTreeUI$MouseHandler.handleSelection(Basic
TreeUI.java:2840)
        at javax.swing.plaf.basic.BasicTreeUI$MouseHandler.mousePressed(BasicTre
eUI.java:2801)
        at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
2)
        at java.awt.Component.processMouseEvent(Component.java:5097)
        at java.awt.Component.processEvent(Component.java:4897)
        at java.awt.Container.processEvent(Container.java:1569)
        at java.awt.Component.dispatchEventImpl(Component.java:3615)
        at java.awt.Container.dispatchEventImpl(Container.java:1627)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483
)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3195)

        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
        at java.awt.Container.dispatchEventImpl(Container.java:1613)
        at java.awt.Window.dispatchEventImpl(Window.java:1606)
        at java.awt.Component.dispatchEvent(Component.java:3477)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:201)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


重现代码
---------- BEGIN SOURCE ---------- import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; public class TreeDemo implements TreeSelectionListener { private JTree tree; private JFrame frame; public TreeDemo() { //Create the nodes. DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); top.add(new DefaultMutableTreeNode("Books for Java Programmers")); tree = new JTree(top); tree.getSelectionModel().setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION); //Listen for when the selection changes. tree.addTreeSelectionListener(this); //Create and set up the window. frame = new JFrame("TreeDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(tree); //Display the window. frame.pack(); frame.setVisible(true); } public void valueChanged(TreeSelectionEvent e) { tree.updateUI(); } public static void main(String[] args) { TreeDemo td = new TreeDemo(); } } ---------- END SOURCE ---------- 原因及解决: This behavior is not something that we can support.
Calling updateUI causes the tree's UI to be uninstalled and then a new one installed.
The developer is calling this method in the middle of events being dispatched
, some of which go to the UI. Because the events are already being dispatched,
they will still go to the uninstalled UI which no longer has reference to the tree
. The only way we could alleviate this problem would be to scatter numerous null checks
throughout all of the UI classes. Doing this would be nonsensical. Will not fix.
If the developer really wants this behavior,
they can wrap the call to updateUI in a SwingUtilities.invokeLater() .

你可能感兴趣的:(JTree.updateUI() during TreeSelectionEvent.valueChanged() causes Null Pointer)