Skip to content
Advertisement

while Converting Object[] to String[] iam getting java.lang.ArrayStoreException for the following code

JavaScript

Advertisement

Answer

This happens due to the following line:

JavaScript

The method Javadoc states that the method throws an ArrayStoreException if an element copied from original is not of a runtime type that can be stored in an array of class newType.

So if the Object[] array contains an element that is not a String, this exception will be thrown. Instead of casting the element to a String, you can get its string representation using the toString() method:

JavaScript

EDIT:

Without streams…

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement