Java:JTable更改侦听器,“对象无法转换为字符串"错误 [英] Java: JTable change listener, "Object cannot be converted to string" error

查看:74
本文介绍了Java:JTable更改侦听器,“对象无法转换为字符串"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable和一个应该控制程序对各种动作的响应的类,我要编写的第一个对象是单元格更改事件.

I have a JTable, and a class which is supposed to control to the program's response to various actions, the first of which I am writing is for a cell change event.

Part对象生成每一行,并相应地设置每一列的列类.

Each row is generated from a Part object, and the column class of each column is set correspondingly.

Part(String partName, String make, String partNumber,
        String altPartNumber, Double price, Integer quantity,
        String description, Boolean isAutomotive, Boolean isMarine,
        Boolean isIndustrial)
{
    //...code not shown...
}

零件对象存储在可序列化的ArrayList<Part>中.

Part objects are stored in a serializable ArrayList<Part>.

本质上,事件处理代码需要做的是使用从JTable获得的更新数据来更新此ArrayList中特定零件对象的参数值.

Essentially, what the event handling code needs to do is update the value of a parameter of a particular part object within this ArrayList using the updated data obtained from the JTable.

下面的代码演示了我如何执行此操作;在这里出现标题中提到的错误.谁能解释如何解决这个问题?

Below is the code that demonstrates how I want to do this; herein the error mentioned in the title occurs. Can anyone explain how to deal with this problem?

public class EventController extends UI implements TableModelListener
{ 
    // Declarations:

    private int row;
    private int column;

    private Part partToChange;

    // ...Omitted for brevity...
    private String updatedName;
    private String updatedMake;
    private Integer updatedQuantity;
    // Don't need declarations for the booleans, can just toggle them.

    @Override
    public void tableChanged(TableModelEvent e)
    {
        // Find out where the change took place...
        this.column = e.getColumn();
        this.row = e.getFirstRow();
        // Get the new value...

        // Send the new value to parts...
        partToChange = data.getPart(row);
        switch(column)
        {
            case 0:
                updatedName = getTableModel().getValueAt(row, column); 
                // ERROR OCCURS HERE ^
                partToChange.setName(updatedName); 
        }
    }
}

推荐答案

您的getTableModel().getValueAt(row, column);返回一个对象引用而不是一个String,但是您正在将其分配给String变量.您的选择包括:

Your getTableModel().getValueAt(row, column); returns an Object reference and not a String, but you're assigning it to a String variable. Your choices include:

  • 您可以将返回的内容转换为字符串,但是如果转换不正确(实际上不是字符串),则可能会出现问题.
  • 或者您可以在其上调用toString().在这里,如果它不是字符串,则不会出现异常,但是返回的字符串可能不是您要使用的字符串.但是,如果变量为null,则会出现问题,并且如果您不首先检查null的情况,则会冒NullPointerException的风险.我建议您走这条路.
  • 或根据 Richard Walton ,使用String.valueOf()可以避免.谢谢理查德!
  • You could cast what is returned to a String, but you risk problems if your cast is incorrect, if it in fact is not a String.
  • Or you could call toString() on it. Here if it's not a String, you won't get an exception, but the String returned might not be what you want to use. A problem occurs however if the variable is null and you risk a NullPointerException if you don't check for null first. I recommend that you go this route.
  • Or as per Richard Walton, by using String.valueOf() you would avoid the null exception issue of toString(). Thank you Richard!

这篇关于Java:JTable更改侦听器,“对象无法转换为字符串"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆