[java.lang.String; cannot be cast to java.lang.String
Asked Answered
U

3

6

I'm obtaining a Vector from a product's API.

Vector<?> dataVector = dataAPI.getReturnVector();

The vector is expected to contain Strings as value. I'm able to print the size of the vector as 2. But for some reason I'm not able to iterate and print the values.

I tried

Iterator<?> iter = dataVector.iterator();

while( iter.hasNext()) {
    System.out.println(iter.next());
}

I always end up getting a

[java.lang.String; cannot be cast to java.lang.String

I used

iter.next().getClass().getName() 

and it turned out to be java.lang.String only.

I googled a bit and found a similar problem at http://prideafrica.blogspot.com/2007/01/javalangclasscastexception.html

I tried to set the generics as String[], but ended up with the same error.

If the vector contains java.lang.String, why do I get this cast exception? How can I print the actual values?

Kindly provide your suggestions.

Underwrite answered 1/2, 2012 at 15:45 Comment(7)
Where did that semicolon come from?Gigot
Did the error message perhaps start with an open brace like [java.lang.String;? That would be the internal name of a String array.Shepard
Oh yes, it started with an open brace[. Sorry for missing that. I didn't know that it would make a difference.Underwrite
What is doing the casting? Is it the iterator?Temperament
Which kind of "product API" is that? May there be a Java bytecode version mismatch?Gati
@βнɛƨнǤʋяʋиɢ Iterator does the casting.Underwrite
@Gati Its a networking product API, and I was able to use the other API's without any problem. The javadocs doesn't say much too...Underwrite
M
7

So the API is not returning a Vector of Strings but a Vector of String[].

You should be able to iterate through the vector and then, for each element, loop through the array.

Iterator<String[]> iter = dataVector.iterator();

while( iter.hasNext()) {
    String[] array = iter.next();
    for(int i=0; i < array.length; i++)
    {
       System.out.println(i + ": " + array[i]);
    }
}
Materially answered 1/2, 2012 at 16:10 Comment(1)
Thanks! This will work. in my case, as you said the Vector was returning String[]. For some reasom, the api method did not 'fill in' the String arrays as expected...Underwrite
L
4

Try compare their classLoaders. If they are different, then this Exception occur.

StringClass1.getClassLoader()==StringClass2.getClassLoader();
Lindeman answered 1/2, 2012 at 15:56 Comment(1)
Looks like something is trying to cast an array of Strings to String.Temperament
P
1

No need to use an iterator. You could just use the elementAt(index) method of Vectors to print the values. Use a For loop to get the indices of the Vector.

Example:

Vector<?> dataVector = dataAPI.getReturnVector();
for(int i = 0; i < dataVector.size(); i++) {
    System.out.println(dataVector.elementAt(i));
}

If you are getting a strange answer (of numbers and letters), you getting a String[] object. That means you will have use the built-in method of Arrays to print the String[] array. See the comments below.

Panga answered 1/2, 2012 at 16:2 Comment(5)
It prints [Ljava.lang.String;@25a091[Ljava.lang.String;@50a11aUnderwrite
@whoopy_whale: You can do System.out.println(Arrays.toString(dataVector.elementAt(i)));.Temperament
@βнɛƨнǤʋяʋиɢ To use Arrays.toString, I had to use the generics as Vector<String[]> and the output was [][]Underwrite
@Panga Tried with Arrays method, and it prints [] [] (empty arrays)Underwrite
Ok...figured out... The API is returning a vector of size two, and the values are two String arrays. Those String arrays are expected to contain values, and in this case for some reason, was empty. Thats a problem with the api call only. But as you all said, the cast exception was due to the String[] and String. Thanks for all the help!Underwrite

© 2022 - 2024 — McMap. All rights reserved.