model.reload() 调用上的 Java SwingWorker NullPointerException

发布于 2024-11-25 06:31:52 字数 1213 浏览 1 评论 0原文

我正在尝试将子节点添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2024-12-02 06:31:52

1) 将所有变量

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Devices");
DefaultTreeModel model = (DefaultTreeModel)this.deviceTree.getModel();

SwingWorker 类移至外部,

不更新 rootmodel,这些 Object 中实际上没有任何内容>,

DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
root.add(child);
model.insertNodeInto(child, root, 0);

仅声明ArrayList; devices = new ArrayList(); 并在 SwingWorkers 结束时将其作为 Object 返回(或 decare 作为私有变量),如果 SwingWokres< /code> 结束然后创建所有 Object

for (String device : devices) {
    DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
    root.add(child);
    model.insertNodeInto(child, root, 0);
}

另一个选项是声明 SwingWorkers#publish()< /a>;,但为什么在这种情况下关心这个

编辑:在此屏幕的左侧(顶部)是标有 SwingWorker 的按钮,读取所有 103 线程

1) move all variables for

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Devices");
DefaultTreeModel model = (DefaultTreeModel)this.deviceTree.getModel();

outside from SwingWorker class

don't update root and model, really nothing from these Object,

DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
root.add(child);
model.insertNodeInto(child, root, 0);

only declare ArrayList<String> devices = new ArrayList<String>(); and return that as Object if SwingWorkers ends (or decare as private variable), if SwingWokres ends then create all Object

for (String device : devices) {
    DefaultMutableTreeNode child = new DefaultMutableTreeNode(device);
    root.add(child);
    model.insertNodeInto(child, root, 0);
}

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 all 103 threads

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文