Search code examples
javajspstruts-1

"cannot be cast to" error raising


I'm working in struts-config 1.2 application. In my struts-config, I have declaration entry for a form and I used this form for some actions. I compiled this objects first with java 1.4 version. That time it was working fine. Now I modified as java version 1.6. I compiled all the objects. Now it is throwing error as my form class cannot be cast to my extend java class. I dont know this is may be due to version change.

Please give me a solution for this, I am struggling with this for 2 days.

My struct-config code is:

<form-bean name="xCustomerPortalForm" type="com.portal.form.XCustomerPortalForm" />
...
<action path="/editXPortalCustomerNew"
        type="com.xmportal.struts.X2AProcessBridgeAction"
        parameter=""
        name="xCustomerPortalForm"
        scope="request"
        validate="false">
  <forward name="success_en" path="/jsp/XMPortalCustomerRegistration.jsp" />
  <forward name="failure_en" path="/jsp/XMPortalCustomerRegistration.jsp" />
</action> 

This is my error message:

java.lang.ClassCastException: com.portal.form.XCustomerPortalForm cannot be cast to com.xmportal.struts.X2AFormBase
    at com.xmportal.struts.X2AProcessBridgeAction.exposeInScope(X2AProcessBridgeAction.java:205)
    at com.xmportal.struts.X2AProcessBridgeAction.checkDataSingle(X2AProcessBridgeAction.java:331)
    at com.xmportal.struts.X2AProcessBridgeAction.checkData(X2AProcessBridgeAction.java:361)
    at com.xmportal.struts.X2AProcessBridgeAction.checkOutcome(X2AProcessBridgeAction.java:618)
    at com.xmportal.struts.X2AProcessBridgeAction.executeLogic(X2AProcessBridgeAction.java:864)
    at org.apache.struts.scaffold.BaseHelperAction.executeLogic(Unknown Source)
    at com.xmportal.struts.X2AProcessBridgeAction.execute(X2AProcessBridgeAction.java:886)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    ...

I am getting error in this object only in X2AProcessBridgeAction

 try {
            X2AFormBase passedForm = (X2AFormBase) form;
            String loginKeyVal= passedForm.getLoginKey();           
                passedForm.set(bean);
            } catch (Exception e) {             
                e.printStackTrace();            

            }

Solution

  • It's simple really,

    In your struts-config.xml, your action (type com.xmportal.struts.X2AProcessBridgeAction) name is xCustomerPortalForm (which is typed to com.portal.form.XCustomerPortalForm). This means, when Struts (ActionServlet) will call your bean com.xmportal.struts.X2AProcessBridgeAction methods, the ActionForm form will always be of type com.portal.form.XCustomerPortalForm.

    So, this typecasting will always fail as it's incompatible type:

    X2AFormBase passedForm = (X2AFormBase) form;
    

    Struts expects you to do this:

    XCustomerPortalForm xCustomPortalForm = (XCustomerPortalForm)form;
    

    I hope, this is clear. Your Action is mapped to only one Form Bean.