class cast Exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahul9885
    New Member
    • Jan 2008
    • 1

    #1

    class cast Exception

    what is class cast exception? why it comes.
    I am getting this exception when i am running my application.
    class files are getting compiled sucessfully.
    Please help.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by rahul9885
    what is class cast exception? why it comes.
    I am getting this exception when i am running my application.
    class files are getting compiled sucessfully.
    Please help.
    Always refuse to write a single line of code if you don't have the Java docs with you.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by rahul9885
      what is class cast exception? why it comes.
      I am getting this exception when i am running my application.
      class files are getting compiled sucessfully.
      Please help.
      Paste your code here.
      I am showing you a example.
      [code=java]
      Vector v = new Vector();
      v.add(new YourClass1());
      YourClass2 obj = v.elementAt(0);
      [/code]

      Now what you would happen?
      Here YourClass1 and YourClass2 are two different classes.

      Debasis Jana.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by dmjpro
        Paste your code here.
        I am showing you a example.
        [code=java]
        Vector v = new Vector();
        v.add(new YourClass1());
        YourClass2 obj = v.elementAt(0);
        [/code]

        Now what you would happen?
        Here YourClass1 and YourClass2 are two different classes.

        Debasis Jana.
        This code doesn't compile. I think you meant for it to compile, but to have a runtime exception. So make the last line of code:

        [CODE=Java]YourClass2 obj = (YourClass2) v.get(0);[/CODE]

        Comment

        Working...