案例:
public void addTreeNode(String userName) {
if(userName.equals("")||userName==null) {
System.out.println("节点名为空或是");
}else {
javax.swing.tree.DefaultMutableTreeNode subMem=new javax.swing.tree.DefaultMutableTreeNode(userName);
this.root.add(subMem);
this.tree.updateUI();//刷新并展开树节点
}
}
在上面的函数中的updateUI方法进行多线程操作的时候,会抛出N多异常,
http://localhost:8010/index.asp4
递归分析开始:.....[2008-11-18 10:27:51]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI.paintRow(BasicTreeUI.java:1498)
at javax.swing.plaf.basic.BasicTreeUI.paint(BasicTreeUI.java:1210)
开始分析链接地址:localhost:8010/index.asp
开始分析链接地址:localhost:8010/gsjs.asp
localhost:8010/gsjs.asp:没有新的链接...
线程处理..........................public/update2.asp
线程处理..........................showgoods.asp?id=5297383
线程处理..........................showgoods.asp?id=5297357
线程处理..........................showgoods.asp?id=5294351
线程处理..........................showgoods.asp?id=5294604
线程处理..........................showgoods.asp?id=5295874
线程处理..........................showgoods.asp?id=5295794
线程处理..........................showgoods.asp?id=5294793
开始分析链接地址:localhost:8010/hsxm.asp
。。。。。。。。。。。。。。。。。。。。。
但运行的结果是没问题的;但有没有什么解决方法?
在网上找到n多类似的问题,都没有解决的方法,后来终于在国外的一个网站上找到解决的方法。
这是作者解决的原文:
Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and this wiki.
We don't see code for your makeDirectory() method and those after it. So we can only talk a general discourse below:
For updateUI() method, which is irrelevant here, read the API documentation of JComponent class.
Having TreeExpansionListener doing initial tree construction is fundamentally wrong because if it doesn't happen to expand, tree's whole branches also do not exist! -- that could be a cause of subtle bug in future application use. The listener should handle particular business logic or GUI task for the application, not a part of constructor work. Initial tree construction should be done at the root node instantiation in the tree constructor.
If you have made and added a new child node to an existing tree node in JTree runtime, you should call defaulttreemodel.nodeStructureChanged() method on the exixting node and tree.expandPath() for the currently selected treepath.
Hope this helps.
P.S. Why do you NOT make a full file system tree here? If you make it, simple recursion could be used.
注意我中间加粗的部分:
public void addTreeNode(String userName) {
if(userName.equals("")||userName==null) {
System.out.println("节点名为空或是");
}else {
javax.swing.tree.DefaultMutableTreeNode subMem=new javax.swing.tree.DefaultMutableTreeNode(userName);
this.root.add(subMem);
this.tree_model.nodeStructureChanged(this.root);
this.tree.expandPath(this.tree.getAnchorSelectionPath());
}
}
结果就出现了喜剧性的变化,一年以来的问题终于解决;
http://localhost:8010/index.asp4
递归分析开始:.....[2008-11-18 10:25:43]
开始分析链接地址:localhost:8010/index.asp
开始分析链接地址:localhost:8010/gsjs.asp
localhost:8010/gsjs.asp:没有新的链接...
线程处理..........................public/update2.asp
线程处理..........................showgoods.asp?id=5297383
线程处理..........................showgoods.asp?id=5297357
线程处理..........................showgoods.asp?id=5294351
线程处理..........................showgoods.asp?id=5294604
线程处理..........................showgoods.asp?id=5295874
线程处理..........................showgoods.asp?id=5295794
线程处理..........................showgoods.asp?id=5294793
开始分析链接地址:localhost:8010/hsxm.asp
线程处理..........................?page=8
线程处理..........................?page=2
线程处理..........................?page=1
线程处理..........................showgoods.asp?id=5296035
线程处理..........................showgoods.asp?id=5296085
线程处理..........................showgoods.asp?id=5296295
...................................
一路跑下来,平平安安的...OK