1 2 Previous Next 16 Replies Latest reply: Nov 13, 2012 1:20 AM by user404 RSS

    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast

    Tshifhiwa
      hi when i run my application am geting this error Caused by: java.lang.ClassCastException: java.lang.String cannot be cast,the line where the error exist is
      public class TestBean {
          
        
          private OrgTypeBean  selectOrgType;
          public void orgtypeChangedListener(ValueChangeEvent valueChangeEvent) {
              selectOrgType = (OrgTypeBean)valueChangeEvent.getNewValue();
              
          }
      Edited by: adf0994 on 2012/11/12 4:18 PM

      Edited by: adf0994 on 2012/11/12 10:01 PM
        • 1. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
          user404
          You got this part wrong :
          List<SelectItem> companiesSelectItems = new ArrayList<SelectItem>();
          for (Company comp: companyList)
          {
          //SelectItem constructor (value, label)
          SelectItem item = new SelectItem(comp, comp.getName());
          companiesSelectItems.add(item);
          }

          In
          SelectItem(...); you have put the wrong Object.
          Should be something like :

          for (OrgTypeBean org: orgList)
          {
          //SelectItem constructor (value, label)
          SelectItem item = new SelectItem(org, org.getName());
          companiesSelectItems.add(item);
          }
          • 2. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
            Tshifhiwa
            am still geting the error,my whole class is
            public class TestBean {
                
                private List<OrgTypeBean> OrgTypeList;
                private List<OrgSubType> OrgSubList;
                private OrgTypeBean  selectOrgType;
                
                public TestBean() {
                    OrgTypeList = new ArrayList<OrgTypeBean>();
                    OrgSubList = new ArrayList<OrgSubType>();
                    
                    OrgTypeList.add(new OrgTypeBean("GOVT","Government"));
                    OrgTypeList.add(new OrgTypeBean("PRIV","Private"));
                    
                    
                    OrgSubList.add(new OrgSubType("GOVT","Communications",""));
                    OrgSubList.add(new OrgSubType("GOVT","Transport","N."));
                    
                    
                    OrgSubList.add(new OrgSubType("PRIV","Partnership","Private Limited"));
                    OrgSubList.add(new OrgSubType("PRIV","Closed Corporation","PRIV"));
                    OrgSubList.add(new OrgSubType("PRIV","Sole Proprietorship","PRIV"));       
                }
            
                public List<SelectItem> getOrgType()
                   { 
                      List<SelectItem> OrgTypeSelectItems = new ArrayList<SelectItem>();
                      for (OrgSubType orgtype: OrgSubList) 
                      { 
                        //SelectItem constructor (value, label)
                        SelectItem item = new SelectItem(orgtype, orgtype.getOrgsubtypename());
                        OrgTypeSelectItems.add(item); 
                     } 
                      return OrgTypeSelectItems; 
                    }
                
                public List<SelectItem> getSubTypeSelectedItem()
                   { 
                      List<SelectItem> subtypeSelectItems = new ArrayList<SelectItem>();
                      if(selectOrgType != null)
                      {
                          for (OrgTypeBean subtype:OrgTypeList) 
                          { 
                            if(subtype.getOrgType() ==  selectOrgType.getOrgType())
                            {  
                               //SelectItem constructor (value, label)
                               //SelectItem item = new SelectItem(emp, emp.getFirstname() +" "+ emp.getLastname());
                               SelectItem item = new SelectItem(subtype,subtype.getDescrtion());
                               subtypeSelectItems.add(item); 
                            }
                          } 
                      }
                      return subtypeSelectItems; 
                    }
                
                public void orgtypeChangedListener(ValueChangeEvent valueChangeEvent) {
                   selectOrgType = (OrgTypeBean)valueChangeEvent.getNewValue();
                      
                }
                
            }
            Edited by: adf0994 on 2012/11/12 4:43 PM

            Edited by: adf0994 on 2012/11/12 4:48 PM

            Edited by: adf0994 on 2012/11/12 10:01 PM
            • 3. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
              user404
              Can you show us the code of your 2 SOC's (of your page)?
              • 4. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                Tshifhiwa
                ok the code is
                public class OrgSubType {
                    
                    private String OrgSubType;
                    private String OrgSubTypename;
                    private String Orgatype;
                    
                    public OrgSubType() {
                        super();
                    }
                
                    public OrgSubType(String orgsubtype, String orgsubtypename, String oargtype) {
                        super();
                        this.OrgSubType = orgsubtype;
                        this.OrgSubTypename = orgsubtypename;
                        this.Orgatype = oargtype;
                    }
                
                    public void setOrgType(String orgtype) {
                        this.Orgatype = orgtype;
                    }
                
                    public String getOrgType() {
                        return Orgatype;
                    }
                
                    public void setOrgSubTypename(String orgsubtypename) {
                        this.OrgSubTypename = orgsubtypename;
                    }
                
                    public String getOrgsubtypename() {
                        return OrgSubTypename;
                    }
                
                    public void setOrgSubType(String orgsubtype) {
                        this.OrgSubType = orgsubtype;
                    }
                
                    public String getorgsubtype() {
                        return OrgSubType;
                    }
                }
                second
                
                
                public class OrgTypeBean {
                    
                    private String  OrgType;
                    private String Description;
                    
                    public OrgTypeBean() {
                        super();
                    }
                
                    public OrgTypeBean(String orgtype, String description) {
                        super();
                        this.OrgType = orgtype;
                        this.Description = description;
                    }
                
                    public void setOrgType(String orgtype) {
                        this.OrgType = orgtype;
                    }
                
                    public String getOrgType() {
                        return OrgType;
                    }
                
                    public void setDescription(String desctiption) {
                        this.Description = desctiption;
                    }
                
                    public String getDescrtion() {
                        return Description;
                    }
                }
                and my lov is
                <td width="50%" height="21"><af:selectOneChoice
                                                        label="#{bindings.LutOrganisationtypesView1.label}"
                                                        required="true"
                                                        shortDesc="#{bindings.LutOrganisationtypesView1.hints.tooltip}"
                                                        id="soc2" simple="true"
                                                        autoSubmit="true"
                                                        immediate="true"
                                                        rendered="true" binding="#{pageFlowScope.orgDetailsBean.orgCat}"
                                                                                  attributeChangeListener="#{pageFlowScope.orgDetailsBean.listener}"
                                                                                  valueChangeListener="#{OrgType.orgtypeChangedListener}">
                                
                                     <af:forEach items="#{bindings.LutOrganisationtypesView1.iteratorBinding.allRowsInRange}"
                                                  var="row">                                 
                                        <af:selectItem id="tiltco2" label="#{row.description}"
                                                       value="#{row.organisationtypecode}"/>
                                      </af:forEach>                 
                                    </af:selectOneChoice>
                                  
                                  </td>
                                </tr><tr>
                                  <td width="50%" height="24">
                                    <af:outputLabel value="Type" id="ol5" showRequired="true"/>
                                  </td>
                                  <td width="50%" height="24">
                                    <af:selectOneChoice id="soc5" partialTriggers="soc2"
                                                        rendered="false">
                                      <f:selectItems value="" id="si2"/>
                                    </af:selectOneChoice><af:selectOneChoice
                                                        label="#{bindings.OrgSubTypeViewVO1.label}"
                                                        required="#{bindings.OrgSubTypeViewVO1.hints.mandatory}"
                                                        shortDesc="#{bindings.OrgSubTypeViewVO1.hints.tooltip}"
                                                        id="soc1" simple="true"
                                                        partialTriggers="soc2"
                                                        autoSubmit="false"
                                                        immediate="false" rendered="true">
                                                         <af:forEach items="#{bindings.OrgSubTypeViewVO1.iteratorBinding.allRowsInRange}"
                                                  var="row">
                                        <af:selectItem id="tilto2" label="#{row.Orgsubtypename}"
                                                       value="#{row.orgsubtypecode}"/>
                                                       <f:selectItems value="#{pageFlowScope.orgDetailsBean.sub}"/>
                                      </af:forEach> 
                                      <f:selectItems
                                                     id="si1"/>
                                    </af:selectOneChoice>
                                  </td>
                Edited by: adf0994 on 2012/11/12 4:46 PM

                Edited by: adf0994 on 2012/11/12 4:54 PM

                Edited by: adf0994 on 2012/11/12 4:55 PM
                • 5. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                  user404
                  I mean the code on your jspx, jsf, ... page.

                  For example:
                  <af:selectOneChoice label="Companies" id="soc1" autoSubmit="true"
                                                                          valueChangeListener="#{viewScope.TestBean.companyChangedListener}">
                                                          <f:selectItems  value="#{viewScope.TestBean.companySelectItems}" id="si1"/>
                                                      </af:selectOneChoice>
                                                      
                                                      <af:selectOneChoice label="Employees" id="soc2" partialTriggers="soc1">
                                                          <f:selectItems value="#{viewScope.TestBean.employeSelectItems}" id="si2"/>
                                                      </af:selectOneChoice>
                  • 6. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                    Tshifhiwa
                    it try this,there is no error but not displaying data
                    <af:selectOneChoice id="soc1" autoSubmit="true"
                                                            valueChangeListener="#{pageFlowScope.OrgType.orgtypeChangedListener}"
                                                            simple="true" immediate="true">
                                                            <f:selectItems
                                                                           value="#{pageFlowScope.OrgType.orgType}" id="si1"/>
                                                        </af:selectOneChoice>
                                                                         
                                      </td>
                                       
                                    </tr><tr>
                                      <td width="50%" height="24">
                                        <af:outputLabel value="Type" id="ol5" showRequired="true"/>
                                      </td>
                                      <td width="50%" height="24">
                                        <af:selectOneChoice id="soc22" partialTriggers="soc1"
                                                            simple="true" immediate="true">
                                                            <f:selectItems value="#{pageFlowScope.OrgType.subTypeSelectedItem}" id="si2"/>
                                                        </af:selectOneChoice>
                    Edited by: adf0994 on 2012/11/12 5:15 PM

                    Edited by: adf0994 on 2012/11/12 5:25 PM

                    Edited by: adf0994 on 2012/11/12 5:36 PM
                    • 7. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                      user404
                      private OrgTypeBean selectOrgType;
                      needs to be
                      private OrgSubType selectOrgType;
                      Since this one point to the object in the first SOC
                      • 8. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                        Tshifhiwa
                        now am able to display first LOV VALUES AND second LOV,but does not display second lov according to first selection
                        public class TestBean {
                            
                            private List<OrgTypeBean> OrgTypeList;
                            private List<OrgSubType> OrgSubList;
                            private OrgSubType  selectOrgType;
                            
                            public TestBean() {
                                OrgTypeList = new ArrayList<OrgTypeBean>();
                                OrgSubList = new ArrayList<OrgSubType>();
                                
                                OrgTypeList.add(new OrgTypeBean("GOVT","Government"));
                                OrgTypeList.add(new OrgTypeBean("PRIV","Private"));
                                
                                
                                OrgSubList.add(new OrgSubType("GOVT","Communications",""));
                                OrgSubList.add(new OrgSubType("GOVT","Transport","N."));
                                
                                
                                OrgSubList.add(new OrgSubType("PRIV","Partnership","Private Limited"));
                                OrgSubList.add(new OrgSubType("PRIV","Closed Corporation","PRIV"));
                                OrgSubList.add(new OrgSubType("PRIV","Sole Proprietorship","PRIV"));       
                            }
                        
                            public List<SelectItem> getOrgType()
                               { 
                                  List<SelectItem> OrgTypeSelectItems = new ArrayList<SelectItem>();
                                  for (OrgTypeBean orgtype: OrgTypeList) 
                                  { 
                                    //SelectItem constructor (value, label)
                                    SelectItem item = new SelectItem(orgtype, orgtype.getDescrtion());
                                    OrgTypeSelectItems.add(item); 
                                 } 
                                  return OrgTypeSelectItems; 
                                }
                            
                            public List<SelectItem> getSubTypeSelectedItem()
                               { 
                                  List<SelectItem> subtypeSelectItems = new ArrayList<SelectItem>();
                                  if(selectOrgType != null)
                                  {
                                      for (OrgSubType subtype:OrgSubList) 
                                      { 
                                        if(subtype.getOrgType() ==  selectOrgType.getOrgType());
                                        {  
                                           //SelectItem constructor (value, label)
                                           //SelectItem item = new SelectItem(emp, emp.getFirstname() +" "+ emp.getLastname());
                                           SelectItem item = new SelectItem(subtype,subtype.getOrgsubtypename());
                                           subtypeSelectItems.add(item); 
                                        }
                                      } 
                                  }
                                  return subtypeSelectItems; 
                                }
                            
                            public void orgtypeChangedListener(ValueChangeEvent valueChangeEvent) {
                               selectOrgType = (OrgSubType)valueChangeEvent.getNewValue();
                                  
                            }
                            
                        }
                        
                        <td width="50%" height="21">
                                            <af:selectOneChoice id="soc1" autoSubmit="true"
                                                                valueChangeListener="#{pageFlowScope.OrgType.orgtypeChangedListener}"
                                                                simple="true" immediate="true">
                                                                 
                                                                <f:selectItems
                                                                               value="#{pageFlowScope.OrgType.orgType}" id="si1"/>
                                                                               
                                                            </af:selectOneChoice>
                                                                             
                                          </td>
                                           
                                        </tr><tr>
                                          <td width="50%" height="24">
                                            <af:outputLabel value="Type" id="ol5" showRequired="true"/>
                                          </td>
                                          <td width="50%" height="24">
                                            <af:selectOneChoice id="soc22" partialTriggers="soc1"
                                                                simple="true" immediate="false">                                         
                                                                <f:selectItems value="#{pageFlowScope.OrgType.subTypeSelectedItem}" id="si2"/>                                       
                                                            </af:selectOneChoice>
                                           
                                          </td>>
                        Edited by: adf0994 on 2012/11/12 9:59 PM
                        • 9. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                          Timo Hahn
                          adf0994,
                          I don't know what you try to do but a look at this code
                              
                          public List<SelectItem> getSubTypeSelectedItem()
                                 { 
                                    List<SelectItem> subtypeSelectItems = new ArrayList<SelectItem>();
                                    if(selectOrgType != null)
                                    {
                                        for (OrgSubType subtype:OrgSubList) 
                                        { 
                                          if(subtype.getOrgType() ==  selectOrgType.getOrgType());
                                          {  
                                             //SelectItem constructor (value, label)
                                             //SelectItem item = new SelectItem(emp, emp.getFirstname() +" "+ emp.getLastname());
                                             SelectItem item = new SelectItem(subtype,subtype.getOrgsubtypename());
                                             subtypeSelectItems.add(item); 
                                          }
                                        } 
                                    }
                                    return subtypeSelectItems; 
                                  }
                          I see a majr problem: the line
                           if(subtype.getOrgType() ==  selectOrgType.getOrgType());
                          should not have the ';' at the end as this means the block beneath the if statement will always run. Next it does not compare the content of the org node type to the sub node type! As far as I see from your other code the type in both classes are String, so you have to use the String.equal() method to compare the types
                           if(subtype.getOrgType().equal(selectOrgType.getOrgType())) == 0
                          {
                          ...
                          }
                          So please first correct your code and see if the problem is gone.

                          Timo
                          • 10. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                            Tshifhiwa
                            thanks timo it works,how can i disable inputtext if is GOVT
                            if(subtype.getOrgType() == selectOrgType.getOrgType());
                            Ii change the code to this but does not disable the inputtext when is govt
                              <af:inputText label="Registration Number " id="it2" columns="20"
                                                              partialTriggers="soc1"                                                                  
                                                              required="#{pageFlowScope.OrgType.orgType!='GOVT'}"
                                                              disabled="#{pageFlowScope.OrgType.orgType=='GOVT'}"
                                                              autoSubmit="true"
                                                              binding="#{pageFlowScope.orgDetailsBean.regNumber}"
                                                              readOnly="false" immediate="true"
                                                              simple="true"/>
                            Edited by: adf0994 on 2012/11/12 8:59 PM
                            • 11. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                              Timo Hahn
                              Instead of using '==' use 'eq' and instead of '!=' use 'ne' ...
                               
                              ...
                              disabled="#{pageFlowScope.OrgType.orgType eq 'GOVT'}"
                              ...
                              Timo
                              • 12. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                                Tshifhiwa
                                like this
                                required="#{pageFlowScope.OrgType.subTypeSelectedItem ne'PRIV'}"
                                                                  disabled="#{pageFlowScope.OrgType.subTypeSelectedItem eq'PRIV'}"
                                • 13. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                                  Timo Hahn
                                  Yes, but you need a space after the 'ne' and 'eq'


                                  Timo
                                  • 14. Re: Caused by: java.lang.ClassCastException: java.lang.String cannot be cast
                                    Tshifhiwa
                                    i still not able to disable regnumber when govement selection is made
                                    <td width="50%" height="22">
                                                        <af:selectOneChoice id="soc1" autoSubmit="true"
                                                                            valueChangeListener="#{pageFlowScope.OrgType.orgtypeChangedListener}"
                                                                            simple="true" immediate="true">
                                                                             
                                                                            <f:selectItems
                                                                                           value="#{pageFlowScope.OrgType.orgType}" id="si1"/>
                                                                                           
                                                                        </af:selectOneChoice>
                                                                                         
                                                      </td>
                                                       
                                                    </tr><tr>
                                                      <td width="50%" height="24">
                                                        <af:outputLabel value="Type" id="ol5" showRequired="true"/>
                                                      </td>
                                                      <td width="50%" height="24">
                                                        <af:selectOneChoice id="soc22" partialTriggers="soc1"
                                                                            simple="true">     
                                                                            
                                                                            
                                                                            <f:selectItems value="#{pageFlowScope.OrgType.subTypeSelectedItem}" id="si2"/>     
                                                                            
                                                                        </af:selectOneChoice>
                                                       
                                                      </td>
                                                    </tr><tr>
                                                      <td width="50%" height="19" nowrap="nowrap">
                                                        <af:outputLabel value="Registration Number " id="ol6"
                                                                        partialTriggers="soc1"
                                                                        showRequired="#{pageFlowScope.OrgType.orgType ne 'PRIV'}"/>
                                                      </td>
                                                      <td width="50%" height="19">
                                                        <af:inputText label="Registration Number " id="it2" columns="20"
                                                                      partialTriggers="soc1"                                                                  
                                                                      required="#{pageFlowScope.OrgType.orgType ne 'GOVT'}"
                                                                      disabled="#{pageFlowScope.OrgType.orgType eq 'GOVT'}"
                                                                      autoSubmit="true"
                                                                      readOnly="false" immediate="true"
                                                                      simple="true"
                                                                      binding="#{pageFlowScope.orgDetailsBean.regNumber}"
                                                                       valueChangeListener="#{pageFlowScope.OrgType.orgtypeChangedListener}"/>
                                                      </td>
                                    i what
                                    if pageFlowScope.OrgType.orgType = gov
                                    required=false
                                    disabled=true
                                    if pageFlowScope.OrgType.orgType = PRIV
                                    REQUIRED=TRUE
                                    disable = false
                                    1 2 Previous Next