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.

7
  • 1
    Where did that semicolon come from? Commented Feb 1, 2012 at 15:49
  • 4
    Did the error message perhaps start with an open brace like [java.lang.String;? That would be the internal name of a String array. Commented Feb 1, 2012 at 15:52
  • Oh yes, it started with an open brace[. Sorry for missing that. I didn't know that it would make a difference. Commented Feb 1, 2012 at 15:59
  • What is doing the casting? Is it the iterator? Commented Feb 1, 2012 at 16:04
  • Which kind of "product API" is that? May there be a Java bytecode version mismatch? Commented Feb 1, 2012 at 16:04

3 Answers 3

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]);
    }
}
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... Commented Feb 1, 2012 at 17:05
4

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

StringClass1.getClassLoader()==StringClass2.getClassLoader();
1
  • Looks like something is trying to cast an array of Strings to String. Commented Feb 1, 2012 at 16:08
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.

5
  • It prints [Ljava.lang.String;@25a091[Ljava.lang.String;@50a11a Commented Feb 1, 2012 at 16:07
  • @whoopy_whale: You can do System.out.println(Arrays.toString(dataVector.elementAt(i)));. Commented Feb 1, 2012 at 16:10
  • 1
    @βнɛƨнǤʋяʋиɢ To use Arrays.toString, I had to use the generics as Vector<String[]> and the output was [][] Commented Feb 1, 2012 at 16:16
  • @Roshnal Tried with Arrays method, and it prints [] [] (empty arrays) Commented Feb 1, 2012 at 16:44
  • 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! Commented Feb 1, 2012 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.