java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
when you try to cast an array kind of a class to the linear type. In this case, attempting to cast String -> String[] or vice versa will cause the JVM to throw this particular exception at runtime. Compilers cannot catch this oversight.
As an example, consider an attempt to read all parameters passed to a servlet using the request object. The parameter map can be obtained with:
Map parameters = request.getParameterMap();
From HTML basics, we know that parameters are passed as key/value pairs of Strings to the web server, so the temptation is to assume that the generics mapping for parameters is
Map<String,String>
for(String key: parameters.keySet()) {
String value = parameters.get(key); // error happens here
System.out.println(key + "=" + value);
}
This code snippet will compile and run fine, producing results that look like:
firstName=[Ljava.lang.String;@1fe49b5
lastName=[Ljava.lang.String;@1993b4f
This is assuming parameters passed to the servlet (or JSP) include ones named 'firstName' and 'lastName', of course.
As it turns out, the value side of the parameter map is actually a String[] type. So the proper generic for this map is
So, just remembering the map's generics mapping may save you a lot of headaches down the road. This is simple Java internals - but a look at forums on the web shows how much of a problem this is for programmers out there. You's get the same kind of exception if you attempted to cast Date to Date[] (which would throw java.lang.ClassCastException: java.util.Date cannot be cast to [Ljava.util.Date;).
77 comments:
Hole in one!
Thanks for the tip, it did solve the mysterious message ;-)
Best wishes,
Marco.
The problem is that most of J2EE is still not generics compatible...
awesomme post!!!
Am really thankful for the tip....it resolved my issue. i wasted 2 weeks to find out the cause....U r the man
This bad boy saved me. Thanks
good tip :) thx
got the exception also with the ParamMap from a Servlet. Thanks for mentioning it.
vv helpful. thx.
nice tip, helped me to solve the problem
AWSOME DUDE!
II love you now1
Thanks alot buddy.. your tip is very much helpful for me. you solved my problem.
Thank you!
That's the "nuts and bolts" that are missing very often ....
:-)
And: it's a pleasure to read blogs that provide well-founded knowledge in a good writing.
Also that is missing in many other places on the web ...
Thanks friend. You saved my time a lot.
Thank you!!!
Thanks.. that solved my problem
Thanks for the help!
1 public String[][] getTableContent(StatelessGuiSubitemTestObject tableObj){
2
3 ITestDataTable currentTable = (ITestDataTable)tableObj.getTestData("contents");
4
5 String tableContents[][] = new String[6][5];
6 int row, col = 0;
7 for (row =1;row < currentTable.getRowCount();row++)
8 {
9 for (col = 1;col < currentTable.getColumnCount();col++)
10 {
11 tableContents[row][col] = currentTable.getCell(row, col).toString();
12 }
13 }
14 return tableContents;
15 }
Here in the 14th step when I try to return the tableContents, I am getting this type of error:[[Ljava.lang.String;@7dcc7dcc
Could you please help me out?
But what is a solution to this problem. I am facing same error, how can i finally get the real value into String object.
Great tip !
I was struggling with that the whole afternoon :)
Cheers.
Jerome
Thanks a lot for the information..
Problem solved..:)
Thanks too!
You helped me saving a lot of time I guess;)
Oh my god this was such an annoying snag. Thanks so much!!!
Thats a great post!!!!
Thanks
Thank you for the tip!
Thanks a lot, i spent long time with this problem
thanks a lot
thanks a lot
Thanks solved my problem....!!!
Ofcourse, as others, You saved my time dude....
Ofcourse, You saved my time dude.... thanks man.
Ofcourse, you saved my time
Thanks Boss. Good Blog. Its helping a lot.
And yes, I also stumbled over this!
Thanks
Thanks a lot! Was just stuck up with this!
I am still a java student and i am running into same problem when i am trying to caputure a request parameter.
I have
pmap.get("query_number");
which i want to be capture as integer. But when i try this
int resultNum=(Integer.parseInt((String)pmap.get(query_number)));
i get:
java.lang.ClassCastException: [Ljava.lang.String;
But when i print out it prints out its value just fine.
Thanks in advance.
Use something like this to get the actual value,
System.out.println(" selectOneRadio3 " + String.valueOf(par.get("selectOneRadio3")));
I am having the same issue and also dud as per the suggestion still no use.. Need help
tring[] split = deptId.split(",");
boolean validParam = false;
// check whether the locationID is a valid locationID for the user
List departments = DierbergsToolsService.fillDeptCosting(connectionString,userName,keyMasterUrl);
Map userDeptList = new LinkedHashMap();
if(split[i].contentEquals((String)userDeptList.get("dept"))){
System.out.println("In Valid ");
validParam =true;
break;
}
And the dept id is in the form of "3, 9". It gives me classcast exception in line for if statement from Short to the String as from DB I think so the values are coming as Short.
thank you! This save my time!
Thanks For the tip.
you're my man! thx a lot.
Thanks,
Laura
Thanks, saved my day too
Thanks. I'd estimate that was around 2 hours of my life you just saved me...
Cheers for that!!!!
Thank you! It just resolved my question. Amazing!
very helpful, thanks for the information.
Thanks, that indeed was helpful!
Thanks, buddy. Got me quickly out of a jam.
Kudos! I pasted my error and this was the first google result, and I was specifically trying to dereference a request/parameterMap entry, so it was spot on.
just what I was looking for. Thanks!
You saved my life ! thanks !
Amazing .. Thanks
Saved my day, Thanks
Nice work. First google result was yours and it was exactly what i was doing wrong. Thanks!
As someone else who has just had hours of his life saved, I'm trying to think if I've seen a more appreciated post!
Thanks!
Man, you're ROCK!!!
Thanks!
Eres lo máximo.
Gracias.
Man, thank you very much. Thanks to you, i solved the problem in 5 minutes. By myself it would have taken weeks to figure it out, cause I was realy clueless.
Thanks Heaps,
Saving my 1 days work.
thnks for the post
Thank you very much!!
Thx Man! Your hint saved me a lot of time.
Greetings from Poland,
PK
Thank you very much.
guy, you saved my night, thanks! =D
Well written, many thanks.
Tricky problem, bloody Map.
Awesome Work dude......good post.
Thanks for the Tip. I was just trying to debug something and this helped fix the getParameterMap !
- Gautham
Thanks Mate. I had already busted my mind for more than a half day in finding the fix for the issue, "struts2 ognl exception for the setter method Ljava.lang.String...". Thanks a lot again for valuable tip
Thanks for sharing. It helped me.
Thanks for the tip. It solved my problems and saved me a bunch of time!
Nice post...!!
Helped me, thank you!
Thank You Very Much... It saved my Hours..
You're my hero!
Thanks, 5 years later still saving me time & teaching me lessons! =)
Awesome. Thank you!
Thanks!
Knowledgeable. Well communicated.
Jubz for the win!
It was a great help.
Post a Comment