This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes getting ClassCastException when using Iterator for a List. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "getting ClassCastException when using Iterator for a List." Watch "getting ClassCastException when using Iterator for a List." New topic
Author

getting ClassCastException when using Iterator for a List.

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050



getting ClassCastException on the line String value=(String)it.next();


http://www.lifesbizzare.blogspot.com || OCJP:81%
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Well, that just means that whatever is in your list, they aren't Strings. Which doesn't surprise me as you're selecting entire rows, not single values. The details of the exception should have told you what they actually are.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

Vishal Hegde wrote:getting ClassCastException on the line String value=(String)it.next();

And quite apart from Matthew's good advice:really isn't much better than:because it doesn't stop your program; and that means that, depending on what you're doing, you could have millions of those messages to plough through.

Until you're ready to publish, or you know that the exception can be recovered from, you're much better off simply letting the program fail - ie, don't use try...catch; or, if you want to avoid throws clauses, simply re-throw the exception as a RuntimeException or java.lang.Error, viz:That way, your program will stop as soon as it hits a problem, and you will always get a stack trace.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050

But I am type casting it to String ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7029
    
  16

Vishal Hegde wrote:But I am type casting it to String ?

Yes, and the reason that you're getting the error is that, as Matthew said, whatever you're casting is NOT a String.

Winston
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Just print out what you are getting on "it.next();" Then according to the output you should cast.


OCPJP
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Some another Lazy way..



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
That is unnecessary; the toString method is called automatically by println, so you can reduce that toAlso, that code will not fail if the Iterator returns null, as you can find out from the println documentation and this method which it calls.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Thanks man i just forgot about that..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You’re welcome
 
Don't get me started about those stupid light bulbs.
 
subject: getting ClassCastException when using Iterator for a List.
 
Similar Threads
How to get List elements
Generics query
runtime exception during accessing elements in hashmap
Hibernate Criteria Query not working properly
how to store map value in string array