I can't understand why i've got ClassCastException at the last row of code
What I understand is MqtAsutrOrgst2
is subclass or implementation type of OrgStructureItemType
class . The List orgstructItems
is defined to keep anything that is a sub type of OrgStructureItemType
which includes MqtAsutrOrgst2
and probably some other sub classes as well which cannot be casted between one another.
MqtAsutrOrgst2 orgStructureItem = (MqtAsutrOrgst2) o;
You are forcing the compiler to believe that at runtime the object o
will be an object of MqtAsutrOrgst2
, but actually it is an object of some other sub class of OrgStructureItemType
which cannot be cast to MqtAsutrOrgst2
.
There are better ways to do this , but you can do a temporary fix :
if(o instanceof MqtAsutrOrgst2)
MqtAsutrOrgst2 orgStructureItem = (MqtAsutrOrgst2) o;
The below code will be a quick fail :
for (MqtAsutrOrgst2 o : OrgStructure) { ... }