• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting ClassCast Exception

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic