As the messages above indicate, the List cannot be differentiated between a List<Object>
and a List<String>
or List<Integer>
.
I've solved this error message for a similar problem:
List<String> strList = (List<String>) someFunction();
String s = strList.get(0);
with the following:
List<?> strList = (List<?>) someFunction();
String s = (String) strList.get(0);
Explanation: The first type conversion verifies that the object is a List without caring about the types held within (since we cannot verify the internal types at the List level). The second conversion is now required because the compiler only knows the List contains some sort of objects. This verifies the type of each object in the List as it is accessed.