Converting an ArrayList to an array raises a java.lang.ArrayStoreException

I have a convertToArray() method that converts an ArrayList to an array. I want to call this method every time an element is added to an ArrayList .

 public class Table extends ArrayList<Row> { public String appArray[]; //Array of single applicant details public String tableArray[][]; //Array of every applicant /** * Constructor for objects of class Table */ public Table() { } public void addApplicant(Row app) { add(app); convertToArray(); } public void convertToArray() { int x = size(); appArray=toArray(new String[x]); } 

}

When I call the addApplication(Row app) method, I get an error: java.lang.ArrayStoreException

So, I changed my addApplicant() method to:

  public void addApplicant(Row app) { add(app); if (size() != 0) convertToArray(); } 

I get the same error message. Any ideas why? I realized if he checks that the ArrayList elements have elements before converting it, should not throw an error?

I can provide a complete error if necessary

+4
source share
1 answer

ArrayStoreException to indicate that an attempt was made to save the wrong type of an object in an array of objects.

So,

 public Row[] appArray; // Row - because you extend ArrayList<Row> public void convertToArray() { int x = size(); appArray = toArray(new Row[x]); } 
+15
source

All Articles