Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (3.9k points)

I try to add objects to a List<String> instance but it throws an UnsupportedOperationException. Does anyone know why?

My Java code:

String[] membersArray = request.getParameterValues('members');

List<String> membersList = Arrays.asList(membersArray);

for (String member : membersList) {

    Person person = Dao.findByName(member);

    List<String> seeAlso;

    seeAlso = person.getSeeAlso();

    if (!seeAlso.contains(groupDn)){

        seeAlso.add(groupDn);

        person.setSeeAlso(seeAlso);

    }

}

The error message:

java.lang.UnsupportedOperationException

    java.util.AbstractList.add(Unknown Source)

    java.util.AbstractList.add(Unknown Source)

    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)

    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

1 Answer

0 votes
by (46k points)

List membersList = Arrays.asList(membersArray);

returns immutable list, what you need to do is

new ArrayList<>(Arrays.asList(membersArray)); to make it mutable

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...