javax.swing.text.SimpleAttributeSet.addAttribute 中的 NullPointerException

发布于 2024-08-28 07:33:15 字数 2139 浏览 8 评论 0原文

有没有人见过这样的异常(JVM 版本为 1.6.0_11)?:

ERROR: java.lang.NullPointerException: null
    at java.util.Hashtable.put(null:-1)
    at javax.swing.text.SimpleAttributeSet.addAttribute(null:-1)
    at javax.swing.text.SimpleAttributeSet.addAttributes(null:-1)
    at javax.swing.text.StyledEditorKit.createInputAttributes(null:-1)
    at javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(null:-1)
    at javax.swing.text.StyledEditorKit$AttributeTracker.caretUpdate(null:-1)
    at javax.swing.text.JTextComponent.fireCaretUpdate(null:-1)
    at javax.swing.text.JTextComponent$MutableCaretEvent.fire(null:-1)
    at javax.swing.text.JTextComponent$MutableCaretEvent.mouseReleased(null:-1)
    at java.awt.AWTEventMulticaster.mouseReleased(null:-1)
    at java.awt.AWTEventMulticaster.mouseReleased(null:-1)
    at java.awt.Component.processMouseEvent(null:-1)
    at javax.swing.JComponent.processMouseEvent(null:-1)
    at java.awt.Component.processEvent(null:-1)
    at java.awt.Container.processEvent(null:-1)
    at java.awt.Component.dispatchEventImpl(null:-1)
    at java.awt.Container.dispatchEventImpl(null:-1)
    at java.awt.Component.dispatchEvent(null:-1)
    at java.awt.LightweightDispatcher.retargetMouseEvent(null:-1)
    at java.awt.LightweightDispatcher.processMouseEvent(null:-1)
    at java.awt.LightweightDispatcher.dispatchEvent(null:-1)
    at java.awt.Container.dispatchEventImpl(null:-1)
    at java.awt.Window.dispatchEventImpl(null:-1)
    at java.awt.Component.dispatchEvent(null:-1)
    at java.awt.EventQueue.dispatchEvent(null:-1)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(null:-1)
    at java.awt.EventDispatchThread.pumpEventsForFilter(null:-1)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(null:-1)
    at java.awt.EventDispatchThread.pumpEvents(null:-1)
    at java.awt.EventDispatchThread.pumpEvents(null:-1)
    at java.awt.EventDispatchThread.run(null:-1)

发生这种情况时,JTextComponent 刚刚被单击(当然不会每次单击它时都会发生)。

我希望我能告诉你一个简单的方法来重现这个,但我不能。它发生在我维护的 Java Swing 应用程序中。这种情况很少发生,而且应用也相当复杂。

我知道仅仅显示这个堆栈跟踪有点不太可能,但我想我会尝试一下。

Has anyone ever seen an exception like this (with version 1.6.0_11 of the JVM)?:

ERROR: java.lang.NullPointerException: null
    at java.util.Hashtable.put(null:-1)
    at javax.swing.text.SimpleAttributeSet.addAttribute(null:-1)
    at javax.swing.text.SimpleAttributeSet.addAttributes(null:-1)
    at javax.swing.text.StyledEditorKit.createInputAttributes(null:-1)
    at javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(null:-1)
    at javax.swing.text.StyledEditorKit$AttributeTracker.caretUpdate(null:-1)
    at javax.swing.text.JTextComponent.fireCaretUpdate(null:-1)
    at javax.swing.text.JTextComponent$MutableCaretEvent.fire(null:-1)
    at javax.swing.text.JTextComponent$MutableCaretEvent.mouseReleased(null:-1)
    at java.awt.AWTEventMulticaster.mouseReleased(null:-1)
    at java.awt.AWTEventMulticaster.mouseReleased(null:-1)
    at java.awt.Component.processMouseEvent(null:-1)
    at javax.swing.JComponent.processMouseEvent(null:-1)
    at java.awt.Component.processEvent(null:-1)
    at java.awt.Container.processEvent(null:-1)
    at java.awt.Component.dispatchEventImpl(null:-1)
    at java.awt.Container.dispatchEventImpl(null:-1)
    at java.awt.Component.dispatchEvent(null:-1)
    at java.awt.LightweightDispatcher.retargetMouseEvent(null:-1)
    at java.awt.LightweightDispatcher.processMouseEvent(null:-1)
    at java.awt.LightweightDispatcher.dispatchEvent(null:-1)
    at java.awt.Container.dispatchEventImpl(null:-1)
    at java.awt.Window.dispatchEventImpl(null:-1)
    at java.awt.Component.dispatchEvent(null:-1)
    at java.awt.EventQueue.dispatchEvent(null:-1)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(null:-1)
    at java.awt.EventDispatchThread.pumpEventsForFilter(null:-1)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(null:-1)
    at java.awt.EventDispatchThread.pumpEvents(null:-1)
    at java.awt.EventDispatchThread.pumpEvents(null:-1)
    at java.awt.EventDispatchThread.run(null:-1)

The JTextComponent was just being clicked on when this happened (it certainly doesn't happen every time you click on it).

I wish I could tell you an easy way to reproduce this, but I can’t. It’s happening in a Java Swing application I maintain. It happens infrequently and the application is quite complex.

I know it’s a bit of a long shot just showing this stack trace, but I thought I’d try.

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

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

发布评论

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

评论(2

坦然微笑 2024-09-04 07:33:15

我从中可以看到的唯一一件事是,“null”已作为插入符号下元素的 InputAttribute 插入。

我的意思是,通过检查这些类的源,我认为问题出现在这一点上:

javax.swing.text.StyledEditorKit.createInputAttributes(null:-1)
javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(null:-1)

从源头来看,此时编辑器工具包从 Document 恢复当前的 Element

摘自 StyledEditorKit:

Element run;
currentParagraph = doc.getParagraphElement(start);
if (currentParagraph.getStartOffset() == start || dot != mark) {
    // Get the attributes from the character at the selection
    // if in a different paragrah!
    run = doc.getCharacterElement(start);
}
else {
    run = doc.getCharacterElement(Math.max(start-1, 0));
}
if (run != currentRun) {
    currentRun = run;
    createInputAttributes(currentRun, getInputAttributes());
}

Element 不为 null,但其属性列表包含一个 null 元素,然后直接在 addAttribute()Hashtable.put(),导致 NPE。

所以在我看来,唯一可能发生不好的事情的地方是在文档中。检查您是否为此 JTextComponent 使用自定义文档,并确保检查内部到底做了什么。

因为从整个事情的追溯来看,只能是这个地方出了问题。事件处理和插入符更新不会对其产生影响,这只是常规更新。之后的部分只是使用传递的对象的结果。所以看起来关键点就在文档中。

The only thing I can see from this, is that a "null" has been inserted as InputAttribute of the Element under the caret.

I mean, from checking sources of these classes, I think the problem occurs at this point:

javax.swing.text.StyledEditorKit.createInputAttributes(null:-1)
javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(null:-1)

From the source, at this point the editor kit recovers the current Element from the Document.

Excerpt from StyledEditorKit:

Element run;
currentParagraph = doc.getParagraphElement(start);
if (currentParagraph.getStartOffset() == start || dot != mark) {
    // Get the attributes from the character at the selection
    // if in a different paragrah!
    run = doc.getCharacterElement(start);
}
else {
    run = doc.getCharacterElement(Math.max(start-1, 0));
}
if (run != currentRun) {
    currentRun = run;
    createInputAttributes(currentRun, getInputAttributes());
}

This Element is not null, but its list of attributes contains a null element, which is then used directly in the addAttribute() and Hashtable.put(), causing the NPE.

So in my opinion, the only place where something bad can happen is in the Document. Check if you are using a custom Document for this JTextComponent, and be sure to check what exactly is done inside.

Because from tracing the whole thing, it can only be a problem at this place. The Event processing and caret update doesn't have an impact in it, it's just a regular update. The part after is only consequence, using the object passed. So it seems like the key point is in the Document.

淡写薰衣草的香 2024-09-04 07:33:15

确保您仅在 Swing 事件调度线程上更新文档。

Make sure you're updating the document only on the Swing Event Dispatch Thread.

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