简体   繁体   English

java.lang.Object无法转换为int

[英]java.lang.Object cannot be converted to int

I have a problem with downloading the number from JTable. 我从JTable下载号码时遇到问题。 In Eclipse I have jre JavaSE 1.7 and it is all ok. 在Eclipse中我有jre JavaSE 1.7,一切都好。 I opened my project in IntelliJ IDEA and chose SDK java jdk 1.8. 我在IntelliJ IDEA中打开了我的项目,并选择了SDK java jdk 1.8。

private int;
public void tableEdit(final JTable table) {

        table.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                // TODO Auto-generated method stub
                if (table.getCellEditor() != null) {

                    int col = table.getSelectedColumn();
                    id =  (int)table.getValueAt(table.getSelectedRow(), 0); //ERROR

Error: 错误:

java: incompatible types: java.lang.Object cannot be converted to int

Edit: 编辑:

New problem: The JTable I have 2 fields, ID and field2 (combobox) after selecting the value from the combobox wants to retrieve a value from the ID field so that they know which row I need to update. 新问题:JTable我有2个字段,ID和field2(组合框),从组合框中选择值后想从ID字段中检索一个值,以便他们知道我需要更新哪一行。

categoryBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                if (newrow_flag == 0) {
                    JComboBox comboBox = (JComboBox) event.getSource();
                    Object item = event.getItem();
                    if (event.getStateChange() == ItemEvent.SELECTED
                            && box_flag_category > 0) {

                        Category selected_category = (Category) categoryBox
                                .getSelectedItem();

                        int rowid = Integer.getInteger(itemTable.getValueAt(
                                itemTable.getSelectedRow(), 0).toString()); //Error

                        id_category = selected_category.getId();


                        fireItemEvent(new ItemsEvent(rowid, "produkty", null,
                                null, null, id_category, id_company, "update"),
                                "box_category");

                    }
                    box_flag_category++;
                }
            }
        });

And error: 并且错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.magazyn.view.View$9.itemStateChanged(View.java:659)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1327)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:834)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    [...]

Error points to this line: 错误指向此行:

int rowid = Integer.getInteger(itemTable.getValueAt(
                                itemTable.getSelectedRow(), 0).toString());

Well look at the error: 那么看看错误:

java: incompatible types: java.lang.Object cannot be converted to int java:不兼容的类型:java.lang.Object不能转换为int

And then look at the line that throws the error: 然后查看抛出错误的行:

id =  (int)table.getValueAt(table.getSelectedRow(), 0);

Now as you can see, you're attempting to cast an Object to an int . 现在您可以看到,您正在尝试将Object转换为int This is not allowed. 这是不允许的。 So you need to be a little more creative: 所以你需要更有创意:

int id = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString()); 

I have a problem with downloading the number from JTable. 我从JTable下载号码时遇到问题。


edit 编辑

re: 回覆:

@christopher, when I chose value from ComboBox i get error Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.magazyn.view.View$9.itemStateChanged(View.java:659) at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223) at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280) @christopher,当我从ComboBox中选择值时,我在线程“AWT-EventQueue-0”中得到错误异常java.lang.ClassCastException:java.lang.Integer无法在com.magazyn.view.View中强制转换为java.lang.String $ 9.itemStateChanged(View.java:659)在javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)

  • don't put JComboBox to JTable, read Oracle tutorial How to use Tables - Using a Combo Box as an Editor for working code example (String instance), model should be store only initial or last selected value from JComboBox as Editor 不要把JComboBox放到JTable上,阅读Oracle教程如何使用表 - 使用组合框作为工作代码示例的编辑器 (String实例),模型应该只存储来自JComboBox的初始或最后选择的值作为编辑器

  • put numbers to JComboBox/DefaultComboBoxModel directly, then returns is number 将数字直接放到JComboBox / DefaultComboBoxModel,然后返回数字

  • TableModelListener firing an event after CellEditor() == null, then code doesn't make me sence TableModelListener在CellEditor()== null之后触发事件,然后代码不会让我发现

See class Jtable Jtable

public Object getValueAt(int row, int column) {
        return getModel().getValueAt(convertRowIndexToModel(row),
                                     convertColumnIndexToModel(column));
    }

Method has return type as Object . 方法的返回类型为Object

use Integer.parseInt(); 使用Integer.parseInt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Object []无法转换为java.lang.String [] - java.lang.Object[] cannot be converted to java.lang.String[] java.lang.object无法转换为(自制类) - java.lang.object cannot be converted to (selfmade class) 不兼容的类型:java.lang.Object无法转换为T - incompatible types : java.lang.Object cannot be converted to T 错误:类型不兼容:java.lang.Object 无法转换为 E - Error: incompatible types: java.lang.Object cannot be converted to E 运算符“+”不能应用于“int”、“java.lang.Object” - Operator '+' cannot be applied to 'int', 'java.lang.Object' 无法将获取java.lang.Object转换为java.lang.Integer - getting java.lang.Object cannot be converted to java.lang.Integer 不兼容的类型:java.lang.Object无法转换为java.lang.String - incompatible types: java.lang.Object cannot be converted to java.lang.String 不兼容的类型:java.lang.Object无法转换为java.lang.String吗? - incompatible types: java.lang.Object cannot be converted to java.lang.String? java.util.Iterator<java.lang.Object[]> 无法转换为 java.util.Iterator<T[]> - java.util.Iterator<java.lang.Object[]> cannot be converted to java.util.Iterator<T[]> 错误:(19、21)java:不兼容的类型:java.lang.Object 无法转换为项目 - Error:(19, 21) java: incompatible types: java.lang.Object cannot be converted to item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM