0

I'm not novice to java , but today i sucked to a problem for 1 hour, i don't know why .

My code looksas follows:

My ArrayList:

    private ArrayList<Theme> selectedThemes;

My Loop:

for (Theme selectedTheme : selectedThemes) {
        System.out.println(selectedTheme.getNom());
    }

When i try to execute the code below , i receive this exception:

java.lang.ClassCastException: java.lang.String cannot be cast to com.mycompany.ecommerce.Modele.Catalogue.Theme

What i understand from the exception is that selectedTheme are String , it is very werd for me.

Note that when i execute this code :

System.out.println(selectedThemes);

it show the object of the list normally.

Edit

I'm not sure if this code could have relation with my issues:it's a SelectManyMenu that has relation with my ArrayList

<p:selectManyMenu id="advanced" value="#{jsfClient.selectedThemes}" 
                                      var="t"  showCheckbox="true">
                        <f:selectItems value="#{jsfClient.themes}" var="theme" itemLabel="#{theme.nom}" itemValue="#{theme}" />
                        <p:column>
                            <h:outputText styleClass="ui-theme ui-theme-#{t.nom}" />
                        </p:column>
                        <p:column>
                            <h:outputText value="#{t.nom}" />
                        </p:column>
                    </p:selectManyMenu>

jsfClient is the ManagedBean. Any Help please , thank you.

user3521250
  • 115
  • 3
  • 14
  • You're iterating across `selectedTheme**s**`, and your `ArrayList` is `selectedTheme`. Was that a typo? And it sounds like you may have a raw type or two somewhere in there because if you have properly genericized code such an exception shouldn't happen because the code wouldn't compile... Can you show more code? I'm not sure that anyone can tell you what's going on with just what you have provided. – awksp Jun 26 '14 at 00:57
  • 2
    It seems like (with that error) you're having a scoping issue with the variables. The exception raised, claims a String is the issue, which leads me to believed that you've accidentally redefined `selectedThemes` as an interable list of strings. – Kodlee Yin Jun 26 '14 at 01:02
  • Kodlee Yin maby you are true, because in jsf , the controller create a tree of component , perhaps the controller create a list of string – user3521250 Jun 26 '14 at 01:12

1 Answers1

0

The problem is in your JSF, more specifically the itemValue="#{theme}" part. JSF is not able to convert the string representation of your theme back to a Theme object.

You either have to implement a custom converter, as described for example here, or, if you don't mind mixing in another library, you can use OmniFaces' SelectItemsConverter.

Some additional possible strategies are also discussed in this blog post by BalusC.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156