7

i have a method convertToArray() which converts an ArrayList to an array. I want to call this method every time an element is added to the 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 the 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 figured if it checks the ArrayList has elements before converting it the error should not be thrown?

I can provide the full error if needed

1

1 Answer 1

19

ArrayStoreException thrown to indicate that an attempt has been made to store the wrong type of object into 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]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.