File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes cast from java.lang.String to (specified) Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "cast from java.lang.String to (specified) Object" Watch "cast from java.lang.String to (specified) Object" New topic
Author

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

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Jun 11, 2002
Posts: 140
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

Joined: Jun 28, 2008
Posts: 580
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

Joined: Jun 28, 2008
Posts: 580
this failure occurs:

Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Have you tried this in your for loop...



?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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
 
subject: cast from java.lang.String to (specified) Object