1
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

3
  • Do you want to convert toMailIds to array? Commented Dec 4, 2013 at 12:26
  • it seems like your toMailIds list does not contain elements of type InternetAddress Commented Dec 4, 2013 at 12:27
  • toMailIds is nothing but list of strings .InternetAddress will accept Strings Commented Dec 4, 2013 at 12:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

address=toMailIds.toArray(); is enough here. But type of address array should be Object[]

Example

List<String> list=new ArrayList<>();
Object[] atr=list.toArray();

1 Comment

toArray() returns Objects and address it of type InetAddress. this will not compile

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.