gpt4 book ai didi

java - 如何找到 Swing 组件抛出的未知异常的(信息性)堆栈跟踪

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:58 26 4
gpt4 key购买 nike

我在我的 Swing 应用程序中设置了一个 UncaughtExceptionHandler,它将捕获并记录我的代码中所有未捕获的异常并将它们报告给我。大多数错误的堆栈跟踪有助于诊断问题。但是,有时我会遇到这样的问题:

Thread : AWT-EventQueue-2
Thread[AWT-EventQueue-2,6,javawsApplicationThreadGroup]

Error Message:
java.lang.Double cannot be cast to java.lang.Integer

Error String:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer

StackTrace: class java.lang.ClassCastException
java.lang.Integer.compareTo(Unknown Source)
javax.swing.table.TableRowSorter$ComparableComparator.compare(Unknown Source)
javax.swing.DefaultRowSorter.compare(Unknown Source)
javax.swing.DefaultRowSorter.access$100(Unknown Source)
javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
java.util.ComparableTimSort.binarySort(Unknown Source)
java.util.ComparableTimSort.sort(Unknown Source)
java.util.Arrays.sort(Unknown Source)
javax.swing.DefaultRowSorter.sortExistingData(Unknown Source)
javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
javax.swing.DefaultRowSorter.toggleSortOrder(Unknown Source)
javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(Unknown Source)
java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
java.awt.Component.processMouseEvent(Unknown Source)
javax.swing.JComponent.processMouseEvent(Unknown Source)
java.awt.Component.processEvent(Unknown Source)
java.awt.Container.processEvent(Unknown Source)
java.awt.Component.dispatchEventImpl(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Window.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.EventQueue.dispatchEventImpl(Unknown Source)
java.awt.EventQueue.access$500(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue.dispatchEvent(Unknown Source)
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.run(Unknown Source)

我不需要诊断这个特定问题的帮助,问题很清楚(用户试图对应用程序中某处的 JTable 进行排序,但该列中的值为 Double s,但表声明表示需要 Integers)。但是,根据此堆栈跟踪,我不知道哪个 JTable 是问题的根源(我的应用程序中有很多表)。在不知道问题出在哪里的情况下,我无法解决它。

那么,我是否可以获得更多有用的调试信息来帮助找到此问题和其他类似问题的根源?

注意:不仅仅是每一行都说“未知来源”,因为堆栈跟踪中列出的类都不是我的类,它们都是库类。因此,即使他们给出了问题的行号,那也是我无法修复的类。

最佳答案

在这种情况下,我会覆盖某些内容以添加调试信息。例如在这种情况下,我们在堆栈跟踪中看到调用了 DefaultRowSorter.setSortKeys,因此我们可以使用一个类

public class MyRowSorter<M, I> extends DefaultRowSorter<M, I> {
....

@Override
public void setSortKeys(List<? extends SortKey> sortKeys) {
try {
super.setSortKeys(sortKeys);
} catch (Exception e) {
//
// print debugging info here!
//
throw e;
}
}
}

现在您可以设置 MyRowSorter 来对您的表格进行排序,将必要的调试信息添加到 MyRowSorter,您可以调试哪个表格有问题。

关于java - 如何找到 Swing 组件抛出的未知异常的(信息性)堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com