• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

class cast exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 8
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
Saloon Keeper
Posts: 7231
169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7231
169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what was the problem with that?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. the problem is solved..
Thanks a lot for all your replies..
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic