I am unable to add an element to a list? UnsupportedOperationException
Arrays.asList() will give you back an unmodifiable list, and that is why your add is failing. Try creating the list with:
AdventureLobbies.players = new ArrayList(Arrays.asList(rs.getString("players").toLowerCase().split(",")));
UnsupportedOperationException when trying to add (Arraylist)
The problem is that you have created your own Arraylist
class (full name agenda.Arraylist
), and your class does not implement the add
operation. This is clear from the stacktrace.
I am guessing that your implementation is something like this:
package agenda;
import java.util.AbstractList;
public class Arraylist<T> extends AbstractList<T> {
...
// No override for `add(<T>)`, `add(int, <T>)`, etcetera.
}
If you don't implement an add
method, the default implementation throws that exception.
Solutions:
- Complete the implementation of your
agenda.Arraylist
class. - Don't implement your own
Arraylist
. Use the standardjava.util.ArrayList
instead. (Unless you have a very good reason, you shouldn't implement your own versions of standard classes.)
UPDATE
(I didn't notice the subtle ArrayList
vs Arraylist
difference. My eyesight isn't as good as it used to be. Sorry. I've updated the above ...)
Your stacktrace says this:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at agenda.Arraylist.size(Arraylist.java:15)
at agenda.AgendaTest.main(AgendaTest.java:40)
C:\Users\melis\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
The second line of that says that the exception is being thrown in a class whose fully qualified name is agenda.Arraylist
.
Note:
- It is NOT
java.util.ArrayList
. - The capitalization of
Arraylist
is ... different. - Looking at your code, I can see that:
- you are using the
Arraylist
class, and - you are calling its
size()
method.
- you are using the
Even if you do import the real ArrayList
class, it will make no difference. You are not using it because you are using the wrong simple name for it.
We have no way of knowing for sure how you got to your current state, but the stacktrace does not lie, and neither does your source code.
(If I was to guess, it would be that at some point you used your IDE's "suggest a correction" functionality to get a fix for an undefined symbol compilation error for your miss-spelled Arraylist
identifier. But you picked the wrong correction ... and your IDE helpfully generated a skeletal implementation of the Arraylist
class rather than spell-correcting the name to ArrayList
and adding the necessary import. But that's just a guess ...)
List.addAll throwing UnsupportedOperationException when trying to add another list
Arrays.asList
returns a fixed sized list backed by an array, and you can't add elements to it.
You can create a modifiable list to make addAll
work :
List<String> supportedTypes = new ArrayList<String>(Arrays.asList("6500", "7600", "8700"));
java.lang.UnsupportedOperationException(Can't add arraylist to list)
As @yassadi already pointed out in one of the comments the problematic part of your code is this line:
List<SalesOrderItem> itemList = Collections.EMPTY_LIST;
Simply change this line to
List<SalesOrderItem> itemList = new ArrayList<>();
and you're done.
As stated in the JavaDocs Collections.EMPTY_LIST
and Collections.emptyList()
are/return immutable lists. That is you must not remove (ok, this is not really the problem with an empty list) or add elements to this list. These helper methods provided by the Collections
class are more to be used as default return values where you want to avoid (memory) overhead by allocating new instances, if only the fact that the collection is empty is of interest.
The immutable characteristic is actually valid for a bunch of methods/members in the Collections
class (emptyList
, singletonList
,emptyMap
, singletonMap
, ...). This always needs to be kept in mind, when using these helper methods.
When using the static members of Collections
, such as EMPTY_LIST
, EMPTY_MAP
, etc. you might notice a compiler warning
Type safety: The expression of type List needs unchecked conversion to conform to List
This is due to the missing type-inference for the static members. By using the corresponding methods such as Collections.emptyList()
the correct generic types will be inferred automatically (emptyList
simply casts to the wanted type) and you will get rid of this annoying warning without adding tons of boilerplate casts to your code. That is the helper methods should always be preferred over the members unless you want pre Java 1.5 compatibility, but these times are hopefully over by now.
See What is the difference between Collections.emptyList() and Collections.EMPTY_LIST
java.lang.UnsupportedOperationException while adding element to a list
You're getting a List
returned by Arrays.asList
, but it's just a wrapper around an array, so you can't add
anything to it.
Returns a fixed-size list backed by the specified array.
If you must add
to it, then create another ArrayList
out of that list.
List<String> l = new ArrayList<String>(Arrays.asList(array));
UnsupportedOperationException When Adding Null to ArrayList
You don't have a java.util.ArrayList
, you have something that implements List
. This particular List
implementation doesn't support modification via changing the size of the List
. Even if you add
an actual String
, you will still get UnsupportedOperationException
. From Arrays.asList
javadocs:
Returns a fixed-size list backed by the specified array.
To be able to add to that List
, wrap it in an actual ArrayList
.
List<String> headers = new ArrayList<>(Arrays.asList(csvBeanReader.getHeader(true)));
Related Topics
Selenium Webdriver How to Resolve Stale Element Reference Exception
Inject an Ejb into Jax-Rs (Restful Service)
Why Does This Java Method Appear to Have Two Return Types
Getting Rsa Private Key from Pem Base64 Encoded Private Key File
Getting Random Numbers in Java
How to Tackle Daylight Savings Using Timezone in Java
What Is the Best Java Image Processing Library/Approach
Does the Jvm Prevent Tail Call Optimizations
Arithmeticexception: "Non-Terminating Decimal Expansion; No Exact Representable Decimal Result"
Converting Symbols, Accent Letters to English Alphabet
Concurrentmodificationexception Despite Using Synchronized
Is It a Bad Practice to Catch Throwable
How to Pass a Parameter to a Java Thread
How to Find the Default Charset/Encoding in Java
How to Respond with an Http 400 Error in a Spring MVC @Responsebody Method Returning String