java

java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method)


int toSize=toMailIds.size();
InternetAddress[] address=new InternetAddress[toSize];
address=toMailIds.toArray(address);

Here toMailIds is arraylist.

I am getting the following exception.

java.lang.ArrayStoreException


Solution

  • You are going to have to use a loop in this case:

    int toSize=toMailIds.size();
    InternetAddress[] address=new InternetAddress[toSize];
    
    for (int i = 0; i < toSize; i++) {
        address[i] = new InternetAddress(toMailIds.get(i));
    }
    

    If a list stores Strings, the toArray method will not create InternetAddress objects from them automatically.