“AWT-EventQueue-0”中的异常

发布于 2024-12-06 03:08:26 字数 938 浏览 0 评论 0原文

我提前道歉这个问题可能很难解释,但我试图包含必要的最少信息(我怀疑你想阅读 10 个不同的类来查找错误)

我一直在做一个简单的(ish) java 中的应用程序,使用 swing 作为 GUI。目前我有一个 JTable、JList 和一个 JButton。在表中双击时,会将其添加到列表中。单击按钮时,自定义的 fileVisitor 会遍历(当前硬编码的)目录,并用它找到的文件填充表。到目前为止,该列表没有任何作用。这一切都按预期进行。

然而,当单击该按钮时,整个应用程序将锁定 fileVisitor 运行约 15 秒。如果有点刺激的话也没关系。当文件树遍历结束时,应用程序再次响应(并且表立即更新所有行)。

所以我决定将 Files.walkFileTree 调用放在它自己的线程中。起初,这似乎是有效的,因为每个文件都被添加到表模型中,并反映在表中(设置为在模型更改时调用重新验证)。但是,如果我在文件树遍历过程中双击某个项目,它将挂起并显示以下消息: java.lang.NullPointerException 从线程“AWT-EventQueue-0”中的 UncaughtExceptionHandler 抛出 java.lang.NullPointerException

在稍微阅读了 swing 后,我认为这是因为我从不是 AWT 调度线程的线程编辑表模型,并立即将编辑表模型的行放入 SwingUtilities.invokeLater(Runnable) 块中。然而,这并没有解决问题。奇怪的是(或者可能不是,这对我来说似乎很奇怪),有时双击会起作用,并且只有在 2 或 3 次尝试后才会导致崩溃。

我的问题是:什么可能导致这种情况?我看不出任何其他明显错误,我所有的谷歌搜索都指向 Swings 缺乏线程安全性,并使用 AWT 调度线程(我认为这是 invokeLater 所做的)。有谁知道出了什么问题吗?

PS 如果有什么不清楚的地方,再次抱歉,而且太长了:P

I appologise in advance for this question possibly being hard to interpret, but I'm trying to include the minimum information necessary (I doubt you want to be reading through 10 different classes looking for the error)

I have been making a simple(ish) application in java, using swing for the GUI. Currently I have a JTable, JList and a JButton. When a row is double clicked in the table, it is added to the list. When the button is clicked, a customised fileVisitor walks through a (currently hard-coded) directory and populates the table with the files it was finding. The list does nothing so far. This all worked as expected.

However, when the button is clicked, the whole application locks up for the ~15s it takes the fileVisitor to run. This is fine if a bit irritating. When the file tree walk ends, the application responds again (and the table updates all rows at once).

So I decided to put the Files.walkFileTree call in its own thread. At first this appeared to be effective, as each file was added to the table model it was reflected in the table (that was set to call revalidate on a model change). However, if I double clicked an item while the file tree walk was in progess, it would hang with the message: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "AWT-EventQueue-0"

After reading up on swing a bit, I assumed it was due to my editing the table model from a thread that was not the AWT dispatch thread, and promptly put the lines that edit the table model in a SwingUtilities.invokeLater(Runnable) block. However, this did not fix the issue. Weirdly (or possibly not, it seems weird to me), sometimes a double-click works, and it's only after 2 or 3 attempts that I cause the crash.

My question is: what can be causing this? I can't see anything else obviously wrong, and all my googling points to Swings lack of thread-safety, and using the AWT dispatch thread (which I thought was what invokeLater does). Anyone know what's wrong?

P.S. sorry again if anything's unclear, and that it's so long :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

是伱的 2024-12-13 03:08:26

事实证明有两个问题:
1)我使用线程来进行文件树遍历,而不是使用 Swing Worker doInBackground;
2)我的双击代码实际上产生了一个空指针,但只有当你在表更新时得到正确的时机时,我现在也修复了这个问题。感谢您的关注。

There were two problems it turns out:
1) I was using a thread for my file tree walk rather than a swing worker doInBackground;
2) my double click code was actually producing a null pointer, but only if you got the timing right while the table was updating, I've fixed that now too. Thanks for looking.

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