-1

I am getting

java.lang.UnsupportedOperationException

exception in List.

Why does this exception occur?

Code

List<String> smsUserList = new ArrayList<String>();
    if (event.getEventTemplate().equalsIgnoreCase(CommunicationConstants.MEMBER)) {
        String testNumbers = env.getRequiredProperty(CommunicationConstants.TEST_SMS_NUMBRES);
        String[] testSmsNumber = testNumbers.split(",");
        if (null != testSmsNumber && testSmsNumber.length > 1) {
            smsUserList = Arrays.asList(testSmsNumber);

        }

    }
    if (event.getEventTemplate().equalsIgnoreCase(CommunicationConstants.AGENT)) {
        String testNumbers = env.getRequiredProperty(CommunicationConstants.TEST_SMS_NUMBRES);
        String[] testSmsNumber = testNumbers.split(",");
        if (null != testSmsNumber && testSmsNumber.length > 1) {
            smsUserList = Arrays.asList(testSmsNumber);
        }
    }

    Set<SMSCommunicationRecipient> smsRecipientAll = event.getSmsCommunicationRecipient();
    for (SMSCommunicationRecipient smsRecipient : smsRecipientAll) {
        String smsRecipientValue = smsRecipient.getRecipientGroupId().getReferenceTypeValue();
        if (smsRecipientValue.equalsIgnoreCase(CommunicationConstants.MEMBER)) {
            List<String> memberContact = (List<String>) communicationInput
                    .get(CommunicationConstants.MEMBER_CONTACT_NUMBER_LIST);
            if (CollectionUtils.isNotEmpty(memberContact)) {
                for (String smsNumber : memberContact) {
                    smsUserList.add(smsNumber);
                }
            }
        }
        if (smsRecipientValue.equalsIgnoreCase(CommunicationConstants.AGENT)) {
            List<String> agentContact = (List<String>) communicationInput
                    .get(CommunicationConstants.AGENT_CONTACT_NUMBER_LIST);
            if (CollectionUtils.isNotEmpty(agentContact)) {
                for (String smsNumber : agentContact) {
                    smsUserList.add(smsNumber);
                }
            }
        }
    }
1
  • May I know, from which line you are getting this .? Commented Jul 25, 2018 at 11:10

1 Answer 1

3

Arrays.asList(testSmsNumber) returns a fixed sized list, so you cannot add elements to it.

Change

smsUserList = Arrays.asList(testSmsNumber);

to

smsUserList = new ArrayList<>(Arrays.asList(testSmsNumber));

Or, since you are already creating an ArrayList with:

List<String> smsUserList = new ArrayList<String>();

change

smsUserList = Arrays.asList(testSmsNumber);

to

smsUserList.addAll(Arrays.asList(testSmsNumber));

Though if you take this second approach, depending on your logic, you might want to call smsUserList.clear() before smsUserList.addAll() (since there are multiple places in your code that assign to the smsUserList variable, so perhaps you want the List to be cleared each time you make that assignment).

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.