This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes I have a ClassCastException as well Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I have a ClassCastException as well" Watch "I have a ClassCastException as well" New topic
Author

I have a ClassCastException as well

mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
When I run my client server project I am getting a ClassCastException error??? My method finds an employee by the ID, Here are the specs I am to use.
variables
ArrayList emps;
Employee emp; I have an Employee class
Test the map for the passed key
if found
set the emps var to the arraylist found by doing a get on the byId map using the passed arg
extract the one employee outof the map using the map's get(0) method
execute the employees toString() to represent this emp as a string and return the stringto the protocol
otherwise return string that says not found

here is the code:



Thank you Oh Lord<br />For the white blind light<br />A city rises from the sea<br />I had a splitting headache<br />from which the future's made<br />--morrisson
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Which line throws the exception? What exactly does it say? Usually the message will identify what class you were attempting to cast to, and what class was actually found. This is really useful information when debugging; don't overlook it.


"I'm not back." - Bill Harding, Twister
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
Here is an updated code version, same error:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Looks like you're adding an ArrayList to an ArrayList, then taking it out and expecting it to have magically transformed into an Employee?
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
Hows this:
It still does not work???
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I have somewhat figured out??? However data is not displaying correctly. Can you look at this please, thank you. Here are the pertinent methods

Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Mike,
When you said the data was not displayed right,
how do you want it display and how was it displayed currently?
-Ryo
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I have a ClassCastException as well
 
Similar Threads
Can't write collection back to disk
ClassCastException while type casting getHibernateTemplate.iterate() to hibernate bean
Problem With ClassCasting
persistence.xml file for MS SQL Server 2000
Problems w/ a HashMap