Results 1 to 7 of 7
  1. #1
    maya.more is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default ClassCast Exception

    I get class cast exception when explicitly casting Double to Int

    This is my code
    List<Integer> NumList




    for (Object Num: NumList){

    if (Num instanceof Integer){

    pList.add((Integer) Num);

    } else {

    Double doubleNum = (Double) Num;
    Integer integerNum = Num.intValue(); (Exception occurs here)
    pList.add(integerNum);

    }
    }


    This works fine in Windows , but causes class cast exception when deployed in Linux Root Cause: java.lang.Double cannot be cast to java.lang.Intege


    Any help is appreciated

    Thanks

  2. #2
    maya.more is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: ClassCast Exception

    small change in code

    for (Object Num: NumList){

    if (Num instanceof Integer){

    pList.add((Integer) Num);

    } else {

    Double doubleNum = (Double) Num;
    Integer integerNum = doubleNum .intValue(); (Exception occurs here)
    pList.add(integerNum);

    }
    }

    Any ideas please?
    Thanks

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: ClassCast Exception

    What is the full text of the error message?

    The line with the comment does not do a cast. The cast is on the preceding line.

  4. #4
    maya.more is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: ClassCast Exception

    This is the stack trace
    Exception thrown < getSize > exception message java.lang.Double cannot be cast to java.lang.Integer with params :: [4.51100104E8]
    java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
    at com.core.dao.daoImpl.convertNumList(daoImpl.java:5 3)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: ClassCast Exception

    java.lang.Double cannot be cast to java.lang.Integer
    That says what the problem is.
    Try finding the correct type by using instanceof before casting.

  6. #6
    maya.more is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: ClassCast Exception

    When I debug and see the varialbles ,its of type Double

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: ClassCast Exception

    If its Double, then there should not be any problem casting it to Double.

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. [SOLVED] JScrollPane - ClassCast Exception
    By thayalan in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2009, 04:52 PM
  4. ClassCast exception with XML files
    By ranjithch in forum XML
    Replies: 0
    Last Post: 01-19-2009, 02:02 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •