0

I have a code as below where I am trying to add an ArrayList(componentList) to another ArrayList(SpareParts). It works fine a long as spareParts list is not empty. When the spareParts List is empty it throws java.lang.UnsupportedOperationException: null

List<ProductReferenceData> spareParts = new ArrayList<ProductReferenceData>();
/**
 Method which fills spareParts List and does not return null. Checked in Debugger as well.
*/
if(componentLabels!=null && !CollectionUtils.isEmpty(componentLabels))
 {
    spareParts.addAll(componentLabels);
 }

I can put an if else loop to check if spareParts list is empty to patch this issue but is there an elegant solution to this? Maybe in Java 8

---edit--- Adding stacktrace

INFO   | jvm 1    | main    | 2020/08/07 20:10:39.309 | java.lang.UnsupportedOperationException: null
INFO   | jvm 1    | main    | 2020/08/07 20:10:39.316 |     at java.util.AbstractList.add(AbstractList.java:148) ~[?:1.8.0_172]
INFO   | jvm 1    | main    | 2020/08/07 20:10:39.316 |     at java.util.AbstractList.add(AbstractList.java:108) ~[?:1.8.0_172]
INFO   | jvm 1    | main    | 2020/08/07 20:10:39.316 |     at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[?:1.8.0_172]
INFO   | jvm 1    | main    | 2020/08/07 20:10:39.316 |     at com.ge.hc.storefront.controllers.pages.ProductPageController.productDetail(ProductPageController.java:418) ~[classes/:?]
antnewbee
  • 1,779
  • 4
  • 25
  • 38
  • 1
    Please provide a full stacktrace and a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) that we can run and figure out what is happening. I suspect that the `UnsupportedOperationException` is due to something in your `CollectionUtils` method rather than the `spareParts.addAll(componentLabels)` call. – Stephen C Aug 08 '20 at 06:38
  • Also, please provide the full stack trace. – Turing85 Aug 08 '20 at 06:40
  • AFAIK, `ArrayList` in Java 8 does not throw `UnsupportedOperationException`. – Stephen C Aug 08 '20 at 06:42
  • 1
    I suspect that some kind of immutable or unmodifyable list is at work here. – Turing85 Aug 08 '20 at 06:42
  • Added the full stacktrace...below that stacktrace is just custom code. – antnewbee Aug 08 '20 at 06:53
  • 1
    What line is line `418` in `ProductPageController`? – Turing85 Aug 08 '20 at 06:59
  • Make sure are you using java 8 in runtime or not? – Eklavya Aug 08 '20 at 07:03
  • Similar to this stack trace. [Java List.add() UnsupportedOperationException - Stack Overflow](https://stackoverflow.com/questions/5755477/java-list-add-unsupportedoperationexception) –  Aug 08 '20 at 07:45
  • Line 418 is `spareParts.addAll(componentLabels);` – antnewbee Aug 09 '20 at 05:33
  • Can you check if method filing `spareParts` returns `Arrays.asList()` – Pramod Aug 10 '20 at 15:15
  • 2
    Then, `spareParts` is not referencing an `ArrayList` at the point of time when the exception happens. Besides that, what’s the point of writing `CollectionUtils.isEmpty(componentLabels)` instead of a straight-forward `componentLabels.isEmpty()`? – Holger Aug 11 '20 at 10:40
  • @Holger- Thanks it turns out method which fills spareParts is actually returning List and not ArrayList maybe that is the problem. Also previously we have seen `componentLabels.isEmpty()` throws NPE if list is null but `CollectionUtils.isEmpty(componentLabels)` returns a proper true/false. – antnewbee Aug 21 '20 at 09:58

0 Answers0