Jens Jens - 12 days ago 7
Java Question

Java: Casting Object to Array type

I am using a web service that returns a plain object of the type "Object". Debug shows clearly that there is some sort of Array in this object (see the image below) so I was wondering how I can cast this "Object" to an Array (or similar)?

Debug Image

I tried the following:

Collection<String> arr = (Collection<String>) values;
Vector<String> arr = (Vector<String>) values;
ArrayList<String> arr = (ArrayList<String>) values;


But nothing worked. I always get an InvocationTargetException.

What am I doing wrong?

Answer

Your values object is obviously an Object[] containing a String[] containing the values.

String[] stringValues = (String[])values[0];