Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ClassCastException

 
renu richard
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't post the same question in multiple forums.

duplicate thread
 
renu richard
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

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

Thanks
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Calendar didn't implement Comparable until Java 5.0, so prior to that version you had to sort using a Comparator object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic