Results 1 to 7 of 7
Thread: ClassCast Exception
- 03-02-2012, 10:33 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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
- 03-02-2012, 10:41 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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
- 03-03-2012, 03:02 AM #3
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.
- 03-05-2012, 08:36 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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)
- 03-05-2012, 08:41 PM #5
Re: ClassCast Exception
java.lang.Double cannot be cast to java.lang.Integer
Try finding the correct type by using instanceof before casting.
- 03-06-2012, 12:02 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: ClassCast Exception
When I debug and see the varialbles ,its of type Double
- 03-06-2012, 12:04 AM #7
Similar Threads
-
unreported exception java.lang.Exception
By Marisabel in forum New To JavaReplies: 6Last Post: 05-01-2011, 04:21 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-15-2011, 12:39 AM -
[SOLVED] JScrollPane - ClassCast Exception
By thayalan in forum AWT / SwingReplies: 2Last Post: 05-24-2009, 05:52 PM -
ClassCast exception with XML files
By ranjithch in forum XMLReplies: 0Last Post: 01-19-2009, 03:02 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 07:41 PM
Bookmarks