This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.lang.ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.ClassCastException" Watch "java.lang.ClassCastException" New topic
Author

java.lang.ClassCastException

renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi,

I have an arraylist "list" which is holding the calendar objects. I need to sort the arraylist. And the code is as belo.

-------->> This way i am setting the calendar objects into the arraylist.

--------->> This is where i am calling the returndate method which will sort the arraylist and return the calendar object.

--------->>This is the method which takes the arraylist as input and it wil sort and send the calendar object which is at the index 0.
During the program execution, when it comes to the above highlighted line, it is giving the below exception

Exception:


Please help....Many Thanks

[ UD: fixed CODE tags ]


Cheers,
Richard
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41133
    
  45
The javadocs for Collections.sort mention the condition in which a ClassCastException is thrown. So, what else is in that list besides that one Calendar object?


Ping & DNS - my free Android networking tools app
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Don't post the same question in multiple forums.

duplicate thread


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi ulf,

After adding these statements



inside the returnDate method. These are the calendar objects available in that list

llogs

first object:java.util.GregorianCalendar[time=1261022447000,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,
useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2009,MONTH=11,WEEK_OF_YEAR=51,WEEK_OF_MONTH=3,DAY_OF_MONTH=17,
DAY_OF_YEAR=351,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=9,
HOUR_OF_DAY=9,MINUTE=30,SECOND=47,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]

second object:java.util.GregorianCalendar[time=1008561647000,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,
useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2001,MONTH=11,WEEK_OF_YEAR=51,WEEK_OF_MONTH=4,DAY_OF_MONTH=17,
DAY_OF_YEAR=351,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=9,
HOUR_OF_DAY=9,MINUTE=30,SECOND=47,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]

Third object:java.util.GregorianCalendar[time=977025647000,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+05:30",offset=19800000,dstSavings=0,
useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2000,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=4,DAY_OF_MONTH=17,
DAY_OF_YEAR=352,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=9,
HOUR_OF_DAY=9,MINUTE=30,SECOND=47,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]

[ UD: removed code tags that screw up the layout ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

renu just log the elements of the list through a loop so that we can see all the elements in the list. Just do this


renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi Ankit ,
I have already added 3 calendar objects in the code . Since i am aware of that i gave something like this.


And even list1.size() is also returning 3 only

Could you please help me out regarding this

Many Thanks
renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi,

One more thing i would like to say......

I am running this in weblogic workshop.......

Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I did a google search for the exception to see what is wrong. I was astonished that this thread came as the first result. Google is fast .

Now coming back to the question, in the code this statement is creating suspicion in my mind.

Date date = (Date)df.parse(tempdate);

Can you tell me what this tempdate is. Are you extracting it from a database?? I may be going in the wrong direction but still if you can confirm it then it would be great...
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32
For me...

following program gives correct output..Output:Compiled and ran against JDK 1.6.0.05

Only thing that I can suspect of is the tempdate format. Can you log values of those and see if they match to what I chose above?


Vijay dharap,
<a href="http://dharapvj.wordpress.com" rel="nofollow">Blog</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

java.util.Calendar didn't implement Comparable until Java 5.0, so prior to that version you had to sort using a Comparator object.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.ClassCastException
 
Similar Threads
PreparedStatement (displaying records)
getting the index of the closest value in an array that is sorted in reverse order
List Duplicated
ensureCapacity() method in ArrayList
default values due to Exhausted Resultset- classcast exception