java.lang.Object 不能转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25524506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.Object cannot be converted to int
提问by lukassz
I have a problem with downloading the number from JTable. In Eclipse I have jre JavaSE 1.7 and it is all ok. I opened my project in IntelliJ IDEA and chose SDK java jdk 1.8.
我在从 JTable 下载号码时遇到问题。在 Eclipse 中,我有 jre JavaSE 1.7,一切正常。我在 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.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());
采纳答案by christopher
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
. This is not allowed. So you need to be a little more creative:
现在如您所见,您正在尝试将 anObject
转换为int
. 这是不允许的。所以你需要更有创意:
int id = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString());
回答by Ankur Singhal
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();
回答by mKorbel
I have a problem with downloading the number from JTable.
我在从 JTable 下载号码时遇到问题。
put integer value directly to JTable/XxxTableModel, to avoid any parsing at runtime
JTable is designated to store various data typesin its XxxTableModel
(for
DefaultTableModel
isn't required) then to override getColumnClass
将整数值直接放入JTable/XxxTableModel,以避免在运行时进行任何解析
JTable 被指定在其 XxxTableModel 中存储各种数据类型
(for
DefaultTableModel
不是必需的) 然后覆盖getColumnClass
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 cannot be cast to java.lang.String at com.magazyn.view.View $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 Editorfor working code example (String instance), model should be store only initial or last selected value from JComboBox as Editor
put numbers to JComboBox/DefaultComboBoxModel directly, then returns is number
TableModelListener firing an event after CellEditor() == null, then code doesn't make me sence
不要将 JComboBox 放到 JTable,阅读 Oracle 教程How to use Tables - Using a Combo Box as an Editorfor working code example (String instance),模型应该只存储 JComboBox 作为编辑器的初始或最后选择的值
将数字直接放入 JComboBox/DefaultComboBoxModel,然后返回数字
TableModelListener 在 CellEditor() == null 之后触发事件,然后代码不让我感觉