• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error casting  RSS feed

 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! i dont understand why im getting java.lang.ClassCastException: java.lang.Integer cannot be cast to Clientes.ModeloCliente, when i cast it like this:

ModeloCliente mc = (ModeloCliente) tablaMod.getModel().getValueAt(tablaMod.getSelectedRow(), 0);

my arraylist is: private ArrayList <ModeloCliente> clientes;

and in mi class ModeloCliente i have a constructor with parameters,getters and setters

What's wrong there? i cant get it :S
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You try to cast an Integer to ModeloCliente.
That would mean that getValueAt method returns int or Integer.
I can't tell more because I would need to see more code.
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, my class ModeloCliente is:



and the other getters/setters

From my class i cast it to get the row that i select, then i call another class with another Jdialog:


now in my ClientesModPop i have:

[Added code tags - see UseCodeTags for details]
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code snippet does not contain the declaration of tablaMod, nor is it clear, how its values are populated.
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya, its a JTable

private JTable tablaMod;


The error is throw from cast java.lang.Integer cannot be cast to Clientes.ModeloCliente, im going crazy :S
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getValueAt() method returns whatever the values are within the table model. You still haven't shown us where you create that table model, or where you put values into it, but why do you expect the value to be a ModeloCliente? That would mean that the table contains a ModeloCliente in each cell. Whereas it appears that it contains an Integer in each cell, which is a lot less surprising.

When you create a JTable you tell it what table model to use. If you don't tell it to use something specific, it will use a DefaultTableModel.
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sorry, i try to evade walltext

First of all, thanks

heres my jtable


now i fill it with this method



that cn is from my database conection class calling this method




 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, if zero is the column who has the client code, what's wrong?
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

oh sorry my method in conection class is this:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu nas wrote:oh sorry my method in conection class is this:

Manu,

Could you please read the UseCodeTags (←click) page. It makes your code much easier to read. Matthew has edited your first post, but I think it would be a good idea if you do the rest yourself. Just hit the 'Edit' button on the post and add code tags as described on the page I linked.

Also: you will need to break up some of those long lines. The general rule of thumb for code here is:
80 characters max.

Thanks.

Winston
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah sorry!!! I thought it was automatic.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, if I'm reading that correctly, then when you do this:

You aren't adding a ModeloCliente to the model. And you certainly aren't adding it to the first column. You're extracting the values from a ModeloCliente and adding them across the row. Column 0 contains whatever cl.getCodigo() returns - which is an int (boxed to an Integer). So that's why getValueAt returns an Integer.

To turn it into a ModeloCliente (without more significant changes involving creating your own table model) you'd have to do something similar to what you've done when reading from the database: read the values from each column and create a new ModeloCliente with those values.
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i will try, thank you!
 
manu nas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it passing a hidden column with the object.



Now i can call the Jdialog



Thanks for your patience and dedication guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!