Reputation:
My problem is in this code:
int rowToTable = table.getSelectedRow();
int bookId = (int)table.getValueAt(rowToTable, 0); // line 158
What is the solution?
I need an int
value of bookID, Not Object
.
StackTrace:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at Project.BookPage_Admin.dellAction(BookPage_Admin.java:158)
at Project.BookPage_Admin.actionPerformed(BookPage_Admin.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
Upvotes: 2
Views: 933
Reputation: 12523
try this solution:
int rowToTable = table.getSelectedRow();
int bookId = Integer.valueOf((String)table.getValueAt(rowToTable, 0));
or
int bookId = Integer.parseInt((String)table.getValueAt(rowToTable, 0));
valueOf
does internaly also a parseInt
.
Upvotes: 1
Reputation:
It looks like you have String values in that table. In that case:
int value = Integer.parseInt((String) table.getValueAt(rowToTable, 0));
Upvotes: 2
Reputation: 9946
If you know for sure that the content of that column is integer than instead of type casting to int just use Integer#parseInt()
int bookId = Integer.parseInt((String)table.getValueAt(rowToTable, 0));
Hope this helps.
Upvotes: 1