java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
It happened with me while i was trying to extract a HTTPRequest parameter from an ActionInvocation object. I was writing an interceptor in Struts2 and was trying to check a request parameter. This is how you do it when you are in an interceptor:
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> params = invocation.getInvocationContext().getParameters();
String param_var= params.get("parameter_name");
/*
Other stuff
*/
}
And it gave me following error:
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
According to the Java Doc it should return an Object and when i tried to debug it. I found that it is actually returning an array object. In some cases it may be correct because a request can contain an array against a key. HttpRequest gives two separate function for this but in this case it was only returning an array. Done following modifications to run the code :
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> params = invocation.getInvocationContext().getParameters();
String[] paramArr_var= (String[])params.get("parameter_name");
/*
Other stuff
*/
}
Most Commented Posts
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

03-21-2011 15:08:01 ERROR – RejectedItemsHistoryRepAction : execute : Error while reading config file
java.lang.ClassCastException: com.r2k.ing.bean.RejectedItemsRep incompatible with com.r2k.ing.bean.RejectedItemsHistoryRep
at com.r2k.ing.action.RejectedItemsHistoryRepAction.execute(RejectedItemsHistoryRepAction.java:126)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:966)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:463)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3129)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:238)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:811)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1433)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:93)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:394)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:152)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:213)
at com.ibm.io.async.AbstractAsyncFuture.fireCompletionActions(AbstractAsyncFuture.java:195)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:194)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:741)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:863)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)
03-21-2011 15:08:01 DEBUG – processForwardConfig(ForwardConfig[name=error,path=/error.jsp,redirect=false,contextRelative=false,module=null])