Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.ClassCastException

Status
Not open for further replies.

javaken

Technical User
May 15, 2004
20
BE
Hey,

I have following exception : java.lang.ClassCastException

I've read a file, and put the values in objects of a class.
I've put the objects in a combobox. Now I have a list with items .... that I can select but ....
I want to use a selected item, en put it in a string ...

How to do this??? How to cast???

Thanks!!!

 
Can you post the relevant code ... (get the line number from the exeception)

You cannot cast from Objects that are not in the same hierachy - eg :

Code:
// will not work - 
Object o = new Integer("1");
String s = (String)o;

// Will work
Object o = new String("1");
String s = (String)o;
 
String s = (String)thingsComboBox.getSelectedItem(); gives an error

In class things I have:
ID (== int) + thing (==string) = one object

object is sitting in my combobox.

How can I get the contents of thing1 when item 1 in my comboBox is selected. You to put it in a string???

Thanks ...

 
Quick hack is :

String s = "" +thingsComboBox.getSelectedItem();
 
Sedj,

Thanks for the quick hack ..

Greetz [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top