This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes class cast exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "class cast exception" Watch "class cast exception" New topic
Author

class cast exception

srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
Hi All,

I am trying to develop a simple travel application using Struts and I'm stuck up with conversion of Java String array into object.

I am using split to convert the string into string array and then i need that string array converted to an object of a pojo.

My error is
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.mytravels.pojo.booking.SeatsBlocking

I am stuck up here from past 2 days..
Shankh Pani Parimal
Greenhorn

Joined: Dec 29, 2011
Posts: 8

Need to poper Casting of Object

its Work fine.
srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
Thanks for quick reply Shankh Pani.

It din't help me. Sorry let me clarify my problem to you.
My code is
selectedSeatNos = StringUtil.split(bookingsForm.getSeatNos(), AppConstants.COMMA);
results=Arrays.asList(selectedSeatNos);
List<SeatsBlocking> seatNos= new ArrayList<SeatsBlocking>(results);
blocking.setSeatNos(results.toString());
bookingsForm.setSeatRecords(seatNos);

here the results is a array list which i need to set to a list of type SeatsBlocking.
This is where actually i am getting error... Please let me know if there is a problem in my code..
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
srikanth sridhar wrote:

List<SeatsBlocking> seatNos= new ArrayList<SeatsBlocking>(results);



You can't do that. SeatsBlocking and String are two different types and I assume there is no relationship between them. Both your lists, results as well as seatNos are already typified as String list and SeatsBlocking list so Java's strong type checking wont allow that.
srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
Hi Barney,

yes you are right. But is there any solution to it.?

It will be a great help..
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Ok so you have a String list called results from which you want to make a list of SeatsBlocking. Why don't you loop through results and create as many SeatsBlocking objects as there are in results list. Probably you can assign each element of result into a member within SeatsBlocking instance.
srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
barney,

I tried as you said. But i am not able to create the objects as you said.

Could you please help me in doing so..

Waiting for your reply..

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
srikanth sridhar wrote:But i am not able to create the objects as you said.

Why not? What did you try, and how did it not work?
srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
I did this way

String selectedSeatNos[] = StringUtil.split(bookingsForm.getSeatNos(), AppConstants.COMMA);
for (int i = 0; i < selectedSeatNos.length; i++) {
blocking.setSeatNos(selectedSeatNos[i]);
blockedSeatNos.add(blocking);
bookingsForm.setRecordList(blockedSeatNos);
}
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
And what was the problem with that?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Where is the local variable blocking defined? It should be set to a new instance of SeatsBlocking within the for loop.
Also, the line

should be outside and after the for loop when all entries have been added to blockedSeatNos.
srikanth sridhar
Greenhorn

Joined: Dec 17, 2011
Posts: 15
Thank you. the problem is solved..
Thanks a lot for all your replies..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class cast exception
 
Similar Threads
Cast Object to Object[]
Casting issue
Printing out Ascii charactors
Making a String[] from ResultSet
Passing an array into a vararg method (in Reflection API)