-
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();
-
your code is incorrect
when you do "ite.next" you are calling a new object inside of the list
-
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;
}