Results 1 to 7 of 7
-
Why am I getting this Unsupported Operation Exception?
This error is new to me and I can't quite grasp why it occurs. Here are my error messages:
And this is the code:Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at ozzypos.Products.addNewPrice(Products.java:49)
at ozzypos.ozzyPosGUI.initProducts(ozzyPosGUI.java:60 60)
...
This method was invoked by this code:Java Code://Products.java 48 public void addNewPrice(double newPrice, boolean newPpkg, String newPDesc) { 49 productPrices.add(new ProductPrices(newPrice, newPpkg, newPDesc)); 50 }
Java Code://ozzyPosGUI.java 6059 Products.Product p1 = myProducts.addNewProduct("Product Name", 55.55, false, "Bronze", Products.ProductGroups.GROUP_2); 6060 p1.addNewPrice(99.99, false, "Silver");
I had a feeling that it was because Product p1 is not a reference of the product I mean to edit, please look at the addNewProduct method to see what I mean:
Java Code://Products.java 87 public Product addNewProduct(String pName, double pPrice, boolean pPpkg, String pDesc, ProductGroups pGroup) { 88 Product p = new Product(); 89 p.setProductDescription(pName); 90 p.setProductPrices(new ProductPrices(pPrice, pPpkg, pDesc)); 91 p.setProductGroups(pGroup); 92 productSet.add(p); 93 return p; 94 }
Any clue anyone? :confused:Last edited by ozzyman; 04-11-2011 at 06:29 PM.
- 04-11-2011, 06:35 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
Wrong parts of the code; look at line 49 of your method addNewPrice( ... ). It directly calls method add( ... ) in the AbstractList class; an abstract list doesn't know how to add an element to itself and it is supposed to be overridden. Are you by any chance extending the AbstractList class yourself?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Thanks again JosAh.
Oh I see... so I think its a problem translating the List, because I am not extending the List class, because my field and constructor makes it an ArrayList, which has an add(Object) method.
Java Code:private List<ProductPrices> productPrices; Product() { productPrices = new ArrayList<ProductPrices>(); }
So what I now think is going on is that when I do this:
Java Code:public void setProductPrices(ProductPrices... prices) { productPrices.clear(); productPrices = Arrays.asList(prices); }
...Then it stops being an ArrayList?
-
Also the first price is added fine with the 'addNewProduct(...)' method but its when I try to add the second price with 'addNewPrice' I get this error, and I've even printed all the products with the prices with pricesList.get(0).getPrice() and it was fine.
-
Yeap, that was the problem.
This longer-winded 'setProductPrices' method straightened the problem out:
Problem solved :).Java Code:public void setProductPrices(ProductPrices... prices) { List<ProductPrices> newProductPrices = new ArrayList<ProductPrices>(); newProductPrices.addAll(Arrays.asList(prices)); productPrices.clear(); productPrices = newProductPrices; }
- 04-11-2011, 07:25 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,414
- Blog Entries
- 7
- Rep Power
- 17
Yep, it is an ArrayList but another ArrayList type; i.e. the list is a list that can not change its length and all writes end up in the original array. (see the API documentation for the Arrays class and see the src.zip file for the source of the Arrays class and the nested ArrayList class). The remedy is simple:
This code snippet creates the fixed sized list (see also above) and makes a copy of it again in a new ordinary ArrayList.Java Code:productPrices= new ArrayList(Arrays.asList(prices));
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Similar Threads
-
SAX Parser unsupported characters/encoding
By ShadowBeast in forum Advanced JavaReplies: 3Last Post: 07-13-2010, 09:32 AM -
Default Close Operation
By sehudson in forum AWT / SwingReplies: 3Last Post: 06-01-2010, 08:56 PM -
XOR operation on bytes
By divyanshu023 in forum New To JavaReplies: 1Last Post: 09-17-2009, 07:11 PM -
unsupported protocol
By shiva in forum New To JavaReplies: 1Last Post: 04-09-2009, 09:20 AM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks