model.reload() 调用上的 Java SwingWorker NullPointerException
我正在尝试将子节点添加到 SwingWorker 线程内的 JTree 中。然后重新加载模型以更新 UI。当我重新加载 JTree DefaultModel 时,它抛出 NullPointerException。有办法解决这个问题吗?或者我必须找到不同的方法?
这就是正在发生的事情。
SwingWorker worker = new SwingWorker<String, Void>()
{
@Override public String doInBackground()
{
TimeUnit.SECONDS.sleep(2);
return "hello";
}
@Override public void done()
{
String response = "";
try {
response = this.get();
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
String device = "hello";
ArrayList<String> devices = new ArrayList<String>();
devices.add(device);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Devices");
DefaultTreeModel model = (DefaultTreeModel)this.deviceTree.getModel();
for (String device : devices)
{
DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
root.add(child);
model.insertNodeInto(child, root, 0);
}
// Null pointer exception!!!
model.reload();
}
};
worker.execute();
I am trying to add child nodes to a JTree inside a SwingWorker thread. Then reload the model to update the UI. When I reload the JTree DefaultModel it throws a NullPointerException. Is there a way to fix this? or do I have to find a different approach?
Here is what is happening.
SwingWorker worker = new SwingWorker<String, Void>()
{
@Override public String doInBackground()
{
TimeUnit.SECONDS.sleep(2);
return "hello";
}
@Override public void done()
{
String response = "";
try {
response = this.get();
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
String device = "hello";
ArrayList<String> devices = new ArrayList<String>();
devices.add(device);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Devices");
DefaultTreeModel model = (DefaultTreeModel)this.deviceTree.getModel();
for (String device : devices)
{
DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
root.add(child);
model.insertNodeInto(child, root, 0);
}
// Null pointer exception!!!
model.reload();
}
};
worker.execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 将所有变量
从
SwingWorker
类移至外部,不更新
root
和model
,这些Object
中实际上没有任何内容>,仅声明
ArrayList; devices = new ArrayList();
并在SwingWorkers
结束时将其作为Object
返回(或 decare 作为私有变量),如果SwingWokres< /code> 结束然后创建所有
Object
另一个选项是声明 SwingWorkers#publish()< /a>;,但为什么在这种情况下关心这个
编辑:在此屏幕的左侧(顶部)是标有 SwingWorker 的按钮,读取所有
103
线程1) move all variables for
outside from
SwingWorker
classdon't update
root
andmodel
, really nothing from theseObject
,only declare
ArrayList<String> devices = new ArrayList<String>();
and return that asObject
ifSwingWorkers
ends (or decare as private variable), ifSwingWokres
ends then create allObject
another options is declare SwingWorkers#publish();, but why care about that in this case
EDIT: on the left (top) side on this screen is
button labeled with SwingWorker
, read all103
threads