• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class cast exception

 
srikanth sridhar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to poper Casting of Object

its Work fine.
 
srikanth sridhar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the problem with that?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. the problem is solved..
Thanks a lot for all your replies..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic