ARRAYSTOREEXCEPTION:
On reading the title itself,you would be able to identify that this exception is thrown when storing something(in appropriate) in an array.Lets discuss in detail about this ArrayStoreException in this post.
In java API documentation it is given that,"If we try to store the objects of wrong type into an array of objects then this exception will be thrown".
The fact is that, if we try to store an object of one type into an array of objects of another type then this ArrayStoreException will be thrown.
Here is the simple example which explains this problem:
Object x[]=new String[3];
x[0]=new Integer(10);// compiles without error but throws ArrayStoreException.
Here,In this first statement Object x[]=new String[3]; It constructs an array of objects(x[]) of String type i.e X[] refers to an array of objects of String type.
Now look at the second statement which tries to store the Integer object in to an array of string type. i.e, x[0]=new Integer(10);
You may wonder why this second statement compiles without an error? as it tries to store an integer object into an array object of String type.This explains us that,"arrays tends to enforce their element types at run-time".This is one of the main disadvantage of the arrays and it could be the reason why parameterized arrays are not supported by JVM,Since the erasure property of generics enforce the type constraints at compile time
Have a look at this statements to know why i have said so,
List <Object> l=new ArrayList<String>();// incompatible error
l.add(new Integer(10));
When we use an ArrayList with a parameter type then at the compile time itself we would be able to identify whether the statement will execute without causing an error or not.
I have also included a program that causes this ArrayStoreException as follows.Let us review the program to identify the cause of this error.
PROGRAM:
import java.util.*;
import java.lang.*;
class Vehicle
{
public Vehicle(String arg1,String arg2)
{
Vehiclename=arg1;
Vehiclecolor=arg2;
}
public String getname()
{
return Vehiclename;
}
public String getcolor()
{
return Vehiclecolor;
}
private String Vehiclename;
private String Vehiclecolor;
}
class Truck extends Vehicle
{
public Truck(String a1,String a2)
{
super(a1,a2);
}
public String getname()
{
return super.getname();
}
public String getcolor()
{
return super.getcolor();
}
}
class VDetails
{
public static void main(String args[])
{
Truck s1=new Truck("BENZ","WHITE");
Truck s2=new Truck("BMW","GREY");
Truck s[]={s1,s2};
Vehicle v[]=s;
v[0]=new Vehicle("THUNDERBIRD","SILVER");
}
}
OUTPUT:
Exception in thread "main" java.lang.ArrayStoreException: Vehicle
at VDetails.main(VDetails.java:44)
This program has a parent class Vehicle and a child class Truck.As you see the program,i have tried to make an array object of class Vehicle type to refer to an array object of type Truck.
i.e, Vehicle v[]=s;
Thus this array v[] can only store objects of type Truck. If you try to store an object of other type then this exception would be thrown.
Thus in order to avoid this exception i suggest you to use ArrayList wherever possible rather than using arrays as it will help you identify the error at compile time itself.
4 comments:
thank u
How Can Convert a Integer Value to Object Type?
Thank you! Had two objects named very similarly (SaveModel and SavedSelectionModel)and I didnt check when I wrote the second method.
Silly error, thank you for the post!