This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cast from java.lang.String to (specified) Object

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this in my Entity-Bean (and in my Session-Bean):

private List <Role> roles = new HashSet<Role>();

and this in my sessionBean:

private List<SelectItem> roles;

I retrieved values for Roles from a selectManyCheckbox. But this what I retrieved are java.lang.STRING - Objects:



You see, a User has selected 2 Roles. Now, I want to save these Roles in my List <Role> roles. So I tried it with that:



But the cast does NOT work. what is wrong? Is it possible ???
 
Darryl Nortje
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Nimo,

You should be getting a compiler error saying that you cannot convert from HashSet to List.

java.util.HashSet is not an implementation of java.util.List, it's an implementation of java.util.Set. So you either need to change it to

private Set<Role> roles = new HashSet<Role>();

OR

private List<Role> roles = new ArrayList<Role>();
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeahh..I have tried it with that:

private List<Role> roles = new ArrayList<Role>();

but the same failure occurs...

how can I convert a String-Object to my Role-Object?
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this failure occurs:

 
Darryl Nortje
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried this in your for loop...



?
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I have tried it with that before, but the same failure occurs. It seems, that I cannot cast a String-Object to my Role-Object. I have to extract the values from String and convert it manually..thanks for help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic