Problem description
This exception occurs when you add objects of an incompatible type to an Object[]
array.
Solution
This exception occurs when you add objects of an incompatible type to an Object[]
array. We recommend that you modify the object type based on the stack information.
Sample code
class Father{}
class Son extends Father{}
... ...
public void handleArrayStoreException(){
Father[] fathers=new Son[3];
// The exception is thrown here.
try {
// Parent-class objects cannot be saved and the child-class objects can be saved.
fathers[0]=new Father();
} catch (ArrayStoreException e) {
e.printStackTrace();
}
// Correct code
Father father=new Father();
Son son=new Son();
if(son instanceof Son){
// Objects can be added to the array.
fathers[1]=son;
}
if(father instanceof Son){
// Objects cannot be added to the array.
fathers[2]=father;
}
}