Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2007, 09:30 PM
Ed Ed is offline
Senior Member
 
Join Date: Jun 2007
Posts: 110
Rep Power: 0
Ed is on a distinguished road
Default ClassCastException
I did an iterator
I have different objects
Some of them are cars, and other trucks (both of them are subclass of Vehicles )
When I iterate in every element of the vector
this error appears:

ClassCastException

this is my code

Code:
while it.hastnext()
 
if (it.next() instanceof truck
 
  auxVehicle = (truck)it.next();
 
if (it.next() instanceof Car
 
auxVehicle = (Auto)it.next();
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-04-2007, 06:25 AM
Senior Member
 
Join Date: Jun 2007
Posts: 130
Rep Power: 0
Jack is on a distinguished road
Default
your code is incorrect
when you do "ite.next" you are calling a new object inside of the list
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 06:26 AM
Senior Member
 
Join Date: Jun 2007
Posts: 164
Rep Power: 3
Heather is on a distinguished road
Default
jack is right
I give you an example so you understand
Code:
Object obj = null;
while (it.hasnext()) {
obj = (Vehicle ) it.next();
if (obj instanceof Truck
 
  auxVehicle = (Truck)obj;
 
if (obj instanceof car)
 
auxVehicle = (car)obj;
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ClassCastException in TreeSet pHew New To Java 2 01-16-2008 01:20 AM
Problem with vector, java.lang.ClassCastException paul New To Java 1 07-16-2007 05:31 PM
ClassCastException Felissa New To Java 2 07-04-2007 06:06 AM


All times are GMT +2. The time now is 04:46 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org