Results 1 to 7 of 7
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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:

    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)
    ...
    And this is the code:

    Java Code:
    //Products.java
    48        public void addNewPrice(double newPrice, boolean newPpkg, String newPDesc) {
    49            productPrices.add(new ProductPrices(newPrice, newPpkg, newPDesc));
    50        }
    This method was invoked by this code:
    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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    This error is new to me and I can't quite grasp why it occurs. Here are my error messages:
    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)
    ...
    And this is the code:
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Yeap, that was the problem.

    This longer-winded 'setProductPrices' method straightened the problem out:

    Java Code:
            public void setProductPrices(ProductPrices... prices) {
                List<ProductPrices> newProductPrices = new ArrayList<ProductPrices>();
                newProductPrices.addAll(Arrays.asList(prices));
                productPrices.clear();
                productPrices = newProductPrices;
            }
    Problem solved :).

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    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?
    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:

    Java Code:
    productPrices= new ArrayList(Arrays.asList(prices));
    This code snippet creates the fixed sized list (see also above) and makes a copy of it again in a new ordinary ArrayList.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Ah, I just thought all ArrayLists have unfixed sizes but I've never really looked deep into Lists so thanks for that. And I changed my code again :).

    Cheers.

Similar Threads

  1. SAX Parser unsupported characters/encoding
    By ShadowBeast in forum Advanced Java
    Replies: 3
    Last Post: 07-13-2010, 09:32 AM
  2. Default Close Operation
    By sehudson in forum AWT / Swing
    Replies: 3
    Last Post: 06-01-2010, 08:56 PM
  3. XOR operation on bytes
    By divyanshu023 in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 07:11 PM
  4. unsupported protocol
    By shiva in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 09:20 AM
  5. Replies: 0
    Last Post: 05-26-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •