UnsupportedOperationException at java.util.AbstractList.add

JavaListExceptionAbstract Class

Java Problem Overview


I'm having issues getting a block of code to run properly. I'm not entirely sure WHAT this code does (I'm trying to get a plugin that's out of date to work properly with our server), I just know every 20 minutes it runs and throws out an error. Here's the section of code where the issue is happening:

public class DynamicThread extends Thread {
private LocalShops plugin = null;


public DynamicThread(ThreadGroup tgroup, String tname, LocalShops plugin) {
    super(tgroup, tname);
    this.plugin = plugin;
}

public void run() {
    Map<ItemInfo, List<Integer>> itemStockMap = Collections.synchronizedMap(new HashMap<ItemInfo, List<Integer>>());
    
    //Dump all the shop stock data into the map.
    for ( Shop shop : plugin.getShopManager().getAllShops() ) {
        for ( InventoryItem item : shop.getItems() ) {
            if (itemStockMap.containsKey(item.getInfo()))
                itemStockMap.get(item.getInfo()).add(item.getStock()); //Where error happens
            else
                itemStockMap.put(item.getInfo(), Arrays.asList(item.getStock()));     
        }
    }
    for(ItemInfo item : itemStockMap.keySet()) {
        List<Integer> stockList = GenericFunctions.limitOutliers(itemStockMap.get(item));
        //remove the map before re-adding it
        if (DynamicManager.getPriceAdjMap().containsKey(item)) 
            DynamicManager.getPriceAdjMap().remove(item);
        
        //Get the overall stock change for a given item and then calculate the adjustment given the volatility
        int deltaStock = GenericFunctions.getSum(stockList) - Config.getGlobalBaseStock();
        DynamicManager.getPriceAdjMap().put(item, GenericFunctions.getAdjustment(Config.getGlobalVolatility(), deltaStock)); 
    }
    
    Bukkit.getServer().getScheduler().callSyncMethod(plugin, plugin.getShopManager().updateSigns());
}

}

The error happens from line 42, which is:

                itemStockMap.get(item.getInfo()).add(item.getStock());

The error it outputs happens every 20 minutes twice with 2 seconds in between.

2012-02-16 16:53:25 [INFO] Launch Dynamic Thread
2012-02-16 16:53:25 [SEVERE] Exception in thread "dynamic" 
2012-02-16 16:53:25 [SEVERE] java.lang.UnsupportedOperationException
2012-02-16 16:53:25 [SEVERE] at java.util.AbstractList.add(AbstractList.java:131)
2012-02-16 16:53:25 [SEVERE] at java.util.AbstractList.add(AbstractList.java:91)
2012-02-16 16:53:25 [SEVERE] at       com.milkbukkit.localshops.threads.DynamicThread.run(DynamicThread.java:42)

2012-02-16 16:53:27 [INFO] Launch Dynamic Thread
2012-02-16 16:53:27 [SEVERE] Exception in thread "dynamic" 
2012-02-16 16:53:27 [SEVERE] java.lang.UnsupportedOperationException
2012-02-16 16:53:27 [SEVERE] at java.util.AbstractList.add(AbstractList.java:131)
2012-02-16 16:53:27 [SEVERE] at java.util.AbstractList.add(AbstractList.java:91)
2012-02-16 16:53:27 [SEVERE] at     com.milkbukkit.localshops.threads.DynamicThread.run(DynamicThread.java:42)

Thanks in advance for any help.

Java Solutions


Solution 1 - Java

You're using Arrays.asList() to create the lists in the Map here:

itemStockMap.put(item.getInfo(), Arrays.asList(item.getStock()));  

This method returns a non-resizable List backed by the array. From that method's documentation:

> Returns a fixed-size list backed by the specified array. (Changes to > the returned list "write through" to the array.)

In order to use a resizable List (and actually copy the contents), use the following:

itemStockMap.put(
        item.getInfo(),
        new ArrayList<Integer>(Arrays.asList(item.getStock()))
); 

Note: in general, when seeing that UnsupportedOperationException is being thrown by add, etc. it's typically an indication that some code is trying to modify a non-resizable or unmodifiable collection.

For example, Collections.emptyList or Collections.singletonList (which return unmodifiable collections) may be used as optimizations but accidentally be passed into methods that try to modify them. For this reason it's good practice for methods to make defensive copies of collections before modifying them (unless of course modifying the collection is a method's intended side effect) - that way callers are free to use the most appropriate collection implementation without worrying about whether it needs to be modifiable.

Solution 2 - Java

I think I've worked out your problem. Arrays.asList(item.getStock()) returns a fixed size list based on the Array passed to it.

This means you cannot add more elements to it.

Instead you should do new ArrayList(Arrays.asList(item.getStock())).

This way you are creating a new list that you can add to.

Solution 3 - Java

The problem is you are creating your lists with Arrays.asList. Per the javadoc provided, the returned list is a Fixed Size, therefore add would be unsupported. Wrap the returned list in a copy constructor for arrayList and you should be set.

Solution 4 - Java

In my case I had used:

List<File> removeFilesList= Collections.emptyList();

which made my File arraylist abstract. Used instead:

List<File> removeFilesList= new ArrayList<>();

And the error was fixed.

Solution 5 - Java

List is Interface and you can not Add value in it until it is instance of ArrayList(interface should be implemented by some class)

For Example:

    List<Integer> test = new ArrayList<>();
    test.add(new Integer(2));

    ArrayList<Integer> test2 = new ArrayList<>();
    test2.add(new Integer(2));

    List<Integer> test3 = Collections.EMPTY_LIST;
    test3.add(new Integer(2));

Here Object test and test2 are perfect, because they are object of ArrayList class so addition is possible
While in test3 it is just empty list so you can not add element in it.

I was also doing the same mistake.

Here is my Suggestion Use ArrayList when you have to do operations like add or remove, Use List only for reference purpose.

 Map<ItemInfo, ArrayList<Integer>> itemStockMap = Collections.synchronizedMap(new HashMap<ItemInfo, ArrayList<Integer>>());

Solution 6 - Java

The problem is in the class of the list object that is returned by the get call. It doesn't override the add methods appropriately, and your code is therefore using the placeholder method provided by AbstractList.

There's not much more we can say without knowing what the list class is, and (if it is custom code) seeing the source code.

Solution 7 - Java

I was using a getter to get the list, but it was returning the fixed size of the list and you can't add a new element so,

Instead you should do

new ArrayList(Arrays.asList(item.getStock()))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErickj92View Question on Stackoverflow
Solution 1 - JavaPaul BelloraView Answer on Stackoverflow
Solution 2 - JavaJivingsView Answer on Stackoverflow
Solution 3 - JavaCharlieView Answer on Stackoverflow
Solution 4 - JavaBenjamin RonnelingView Answer on Stackoverflow
Solution 5 - JavaLokesh TiwariView Answer on Stackoverflow
Solution 6 - JavaStephen CView Answer on Stackoverflow
Solution 7 - JavaPratik GargView Answer on Stackoverflow