ClassCastException 的未知来源(在 JTables 中)
我目前正在重构一个 JTable
,它显示多种不同类型的数据。 这种重构的主要原因是存在一些 ClassCastExceptions
(编写代码的作者/朋友已经中断),而且我似乎无法找到这些异常的来源。 由于代码库庞大,我不知道从哪里开始。 有没有人有什么建议? 我意识到这个问题的含糊之处并表示歉意!
我在下面包含了堆栈跟踪。 谢谢!!
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(Unknown Source) at javax.swing.JTable.prepareRenderer(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.BufferStrategyPaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
I'm presently refactoring a JTable
which displays a multitude of different types of data. The primary reason for this refactoring is that there a few ClassCastExceptions
(the author/friend who wrote the code is off on hiatus), and I can't seem to find where these are originating from. Due to the large codebase, I'm at a loss as to where to start. Does anyone have any suggestions? I realize and apologize for the ambiguity of this question!
I've included the stack trace below. Thanks!!
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(Unknown Source) at javax.swing.JTable.prepareRenderer(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.BufferStrategyPaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
很抱歉挖出一个老问题,但我自己也遇到了这个问题。 这篇文章是在搜索中出现的,这就是我遇到的。
我的 JUnits 测试失败了(实际上抛出了运行时异常),但我继续在 JTable 上运行添加/删除(在 JUnit 测试中),这使 GUI 应用程序处于错误状态,并且我会看到 ClassCastException 与 Chris 完全相同曾描述过。
因此,对我来说,“修复”是确保所有单元测试捕获异常并返回失败而不是继续运行更多单元测试。
Sorry to dig up an old question, but I ran into this issue myself & this post came up in a search and this is what I ran into.
I had JUnits tests fail (and actually throwing runtime exceptions) but I continued to run add/removes on my JTable (in the JUnit test) which put the GUI application in a bad state, and I would see the ClassCastException come up exactly as Chris had described.
So the "fix" for me was to make sure that all unit tests catch their exceptions and return failure instead of proceeding to run more unit tests.
我遇到了同样的问题,原因与 Avrom 指定的完全一样。 就我而言,我将
getValueAt
实现为:这里的问题是,如果一行不存在,则为每一列返回一个字符串。 然而,我的一些列具有类类型 Boolean,因此存在例外:
解决方案只是将返回值更改为:
I had the same problem, and the cause was exactly as Avrom specified. In my case, I had the
getValueAt
implemented as:The problem here is that, is a row does not exist, a String is returned, for every column. However some of my columns has the class type, Boolean, and hence the exception:
The solution was simply to change the return value to:
要调试此问题,您可能需要考虑硬着头皮在进行强制转换的
JTable$BooleanRenderer.getTableCellRendererComponent()
行中放置一个断点setSelected((value != null && ((Boolean)value).booleanValue()));
(来自
JTable.java 1.288 06/11/15
)并检查
value 的类类型
。 当您找到字符串
时,您可以从模型中识别出有问题的列和行。 这至少会让您开始识别问题。To debug this problem, you may want to consider biting the bullet and putting a breakpoint in the
JTable$BooleanRenderer.getTableCellRendererComponent()
on the line that makes the castsetSelected((value != null && ((Boolean)value).booleanValue()));
(from
JTable.java 1.288 06/11/15
)and check the class type of
value
. when you find aString
, you can identify the offending column and row from your model. That will at least give you a start on identifying the problem.该表可能包含一个复选框(当列模型声明该列包含布尔类型时),并且渲染器尝试将内容转换为布尔值。 但内容可能只是字符串。 解决方案是更改表中的数据或创建自己的渲染器。
Probably the table contains a checkbox (when the column model states that the column contains type Boolean) and the renderer tries to convert the contents into a boolean. But probably the contents are just strings. The solution is to change the data in the table or to create your own renderer.
我认为问题出在你的 TableModel (jtable.getModel())
它在某处说过
,但此列中模型中的值是一个字符串
I think the problem comes from your TableModel (jtable.getModel())
It said somewhere
but the value in your model in this column is a String
BooleanRenderer
中发生的此错误是因为它期望来自表模型的值是Boolean
类型并尝试转换为它(akf 的答案有它发生的确切代码行)。我的猜测是,最初预计模型将为给定列返回布尔值,但在某一时刻它会返回字符串。
因此,我将集中精力于这个给定的表使用什么模型(它是自定义模型吗?它是向其中添加值的默认模型吗?)并查看它可能在哪里获取 String 而不是
布尔值
。This error which is occurring in
BooleanRenderer
is because it is expecting that the value that is from the table's model is of typeBoolean
and tries to cast to it (akf's answer has the exact line of code where it occurs).My guess is that initially it was expected that the model would return
Boolean
values for the given column but at one point it is returning strings instead.Therefore, I would concentrate on what model is being used for this given table (is it a custom model? Is it the default model where it is adding values to it?) and see where it may be getting a String instead of a
Boolean
.