jsf - ClassCastException Java with list iterator -


i have strange error in code. using iterators since beginning of project , never had problems here can't understand what's going on.

i have model class

public class myobject implements serializable{     private static final long serialversionuid = 1l;      private int field1;     private string field2;     private list<otherobject> field3;     private date field4     ...     // + getters , setters     // + override equals method } 

a class parameters of myobject type

public class fooclass implements serializable{     private static final long serialversionuid = 1l;     private list<myobject> list;     // + getters , setters } 

and have other class using myobject

public class mycontroller {     .....     public static void amethod(fooclass value) {         list<myobject> mylist = value.getlist();         iterator<myobject> iterator = mylist.iterator();         while(iterator.hasnext()) {             myobject temp = iterator.next();  // error on line         }     }     ... } 

my objects displayed in jsf view using following code:

<p:selectcheckboxmenu value="#{fooclass.list}">     <f:selectitems value="#{fooclass.listall}" var="obj"                     itemvalue="#{obj}" itemlabel="#{obj.field2}" /> </p:selectcheckboxmenu> 

here error i'm getting:

java.lang.classcastexception: java.lang.string cannot cast com.myobject 

have got suggestions?

this happens because of 'design' of http protocol: when request sent server data sent strings. so, jsf interprets them strings well, because didn't explicitly tell object expected of particular class, in case myobject. happens because el handles view-model communicaion based on reflection , know generics in java compile-time phenomenon due type erasure: generic information not available @ runtime.

so after submit form list consists of plain string objects , not of myobject instances, seem expect: generic information replaced jsf el after form had been submitted. that's why got classcastexception. can check types of elements list contains if put breakpoint on action(listener) method, or setter method.

to resolve situation either need explicitly tell jsf use converter (by specifying converter attribute, or nesting <f:converter> tag), or switching plain array (to myobject[]) instead of list<myobject>.

you can find more information, solutions, in answer jsf , type safety question.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -