ClassCastException 的未知来源(在 JTables 中)

发布于 2024-07-23 05:26:52 字数 2009 浏览 5 评论 0原文

我目前正在重构一个 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 技术交流群。

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

发布评论

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

评论(6

你的心境我的脸 2024-07-30 05:26:58

很抱歉挖出一个老问题,但我自己也遇到了这个问题。 这篇文章是在搜索中出现的,这就是我遇到的。

我的 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.

心在旅行 2024-07-30 05:26:58

我遇到了同样的问题,原因与 Avrom 指定的完全一样。 就我而言,我将 getValueAt 实现为:

@Override
synchronized public Object getValueAt(int row, int col) {
  if (row < m_rows.size()) {
    return m_rows.get(row).getValueAt(col);
  } 
  else 
  {
    return "";  // THIS IS THE BUG
  }
}

这里的问题是,如果一行不存在,则为每一列返回一个字符串。 然而,我的一些列具有类类型 Boolean,因此存在例外:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
  at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(JTable.java:5409)
  at javax.swing.JTable.prepareRenderer(JTable.java:5736)

解决方案只是将返回值更改为:

return null;

I had the same problem, and the cause was exactly as Avrom specified. In my case, I had the getValueAt implemented as:

@Override
synchronized public Object getValueAt(int row, int col) {
  if (row < m_rows.size()) {
    return m_rows.get(row).getValueAt(col);
  } 
  else 
  {
    return "";  // THIS IS THE BUG
  }
}

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:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
  at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(JTable.java:5409)
  at javax.swing.JTable.prepareRenderer(JTable.java:5736)

The solution was simply to change the return value to:

return null;
小情绪 2024-07-30 05:26:57

要调试此问题,您可能需要考虑硬着头皮在进行强制转换的 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 cast

setSelected((value != null && ((Boolean)value).booleanValue()));

(from JTable.java 1.288 06/11/15)

and check the class type of value. when you find a String, you can identify the offending column and row from your model. That will at least give you a start on identifying the problem.

一场信仰旅途 2024-07-30 05:26:56

该表可能包含一个复选框(当列模型声明该列包含布尔类型时),并且渲染器尝试将内容转换为布尔值。 但内容可能只是字符串。 解决方案是更改表中的数据或创建自己的渲染器。

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.

維他命╮ 2024-07-30 05:26:56

我认为问题出在你的 TableModel (jtable.getModel())
它在某处说过

(..)
public Class<?> getColumnClass(int column)
   {
   switch(column)
     {
   (...)
      case XX: return Boolean.class;
      }
   }

,但此列中模型中的值是一个字符串

public Object getValueAt(int row,int column)
  {
  (..)
   switch(column)
     {
   (...)
      case XX: return (a String);
      }
  }

I think the problem comes from your TableModel (jtable.getModel())
It said somewhere

(..)
public Class<?> getColumnClass(int column)
   {
   switch(column)
     {
   (...)
      case XX: return Boolean.class;
      }
   }

but the value in your model in this column is a String

public Object getValueAt(int row,int column)
  {
  (..)
   switch(column)
     {
   (...)
      case XX: return (a String);
      }
  }
っ左 2024-07-30 05:26:55

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 type Boolean 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.

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