• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting ClassCast Exception  RSS feed

 
Arif Rizwan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi this is arif i am getting classCast Exception at run time following is my code.

here in the above code i am trying to get leaveid by using index i am taking a index into a String variable str and converting into integer integer.parseInt(str)

while converting String to integer i am getting a error classCast Exception how can i resolve this problem?

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arif Rizwan wrote:hi this is arif i am getting classCast Exception at run time following is my code.

here in the above code i am trying to get leaveid by using index i am taking a index into a String variable str and converting into integer integer.parseInt(str)

while converting String to integer i am getting a error classCast Exception how can i resolve this problem?



Can you show us the stacktrace you get from the Exception? Presumably the ArrayList does not contain an ApproveLeaveViewBean at the index you are getting, so when you try and cast the object to that type you get the ClassCast Exception.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two casts in your code, so there are two opportunities for a ClassCastException to happen:

Line 7: If session.getAttribute(LRSConstant.LIST_VB); does not return an ArrayList (perhaps it is some other kind of List, not an ArrayList) then you get a ClassCastException.

Line 9: If list.get(Integer.parseInt(str)); does not return an ApproveLeaveViewBean, you'll get a ClassCastException.

Look closely at the stack trace, it contains valuable information: it tells you exactly where the error occurs and what kind of object was found instead of what was expected.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!