SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-07-2011, 04:28 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
UnsupportedOperation Exception

Gents,

I am getting a Java UnsupportedOperation Exception and I can't figure out why. Details:

I have an instance variable:

Code:
private List<String>    allowedUsers = null;
I initialize it in the constructor:

Code:
allowedUsers = new ArrayList<String>();
I add several elements by converting an Array to a List:

Code:
        String[] StringArray = S.split(D);

        return Arrays.asList(StringArray);
When I try removing an element, I get the exception:

Code:
        if (allowedUsers.contains(userID)) {
            allowedUsers.remove(allowedUsers.indexOf(userID));
        }
I also tried:

Code:
        if (allowedUsers.contains(userID)) {
            allowedUsers.remove(userID);
        }
I've checked for nulls, empty strings, etc.

I know I'm doing something really silly, but what?

Tom
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #2  
Old 02-07-2011, 04:39 PM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
While the toList() doesn't explicitly state that the list is readonly (in terms of adding/removing) elements it does say that changes to the list are reflected in the array. Since you can not grow/shrink the array, i'm guessing the you get UnsupportedOperations when you try to add/remove to the list.

To solve...

Code:
ArrayList list = new ArrayList(Arrays.asList(YourArray));
That will copy your array into a new list that can be modified.
Reply With Quote
  #3  
Old 02-07-2011, 04:39 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
UnsupportedOperation means pretty much what it says. Apparently you're using a nonmutable List implementation that does not support the remove() method. My guess is that it's the List returned from Arrays.asList(), which is not the same as a mutable ArrayList.

The same applies, by the way, to the Lists implemented by my API wrappers. You can index them, search them, and iterate them, but you can't modify them.
__________________
-- Greg
Reply With Quote
  #4  
Old 02-07-2011, 05:40 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Thanks Sean and Greg. Yes, it's because I'm using Arrays.asList, I didn't realize the resulting List was nonmutable. My noob lesson of the day
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #5  
Old 02-07-2011, 06:36 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Quote:
Originally Posted by stuckless View Post
While the toList() doesn't explicitly state that the list is readonly (in terms of adding/removing) elements it does say that changes to the list are reflected in the array. Since you can not grow/shrink the array, i'm guessing the you get UnsupportedOperations when you try to add/remove to the list.

To solve...

Code:
ArrayList list = new ArrayList(Arrays.asList(YourArray));
That will copy your array into a new list that can be modified.
I changed my method to return this:

Code:
return new ArrayList(Arrays.asList(StringArray));
but now I'm getting warnings:

Code:
warning: [unchecked] unchecked call to ArrayList(java.util.Collection<? extends E>) as a member of the raw type java.util.ArrayList
warning: [unchecked] unchecked conversion
Is this normal? When I see warnings I assume I am doing something that I should not be doing.

Here is the entire method:

Code:
    static List<String> delimitedStringToList(String S, String D) {
        List<String> TheList = new ArrayList<String>();

        if (S == null || D == null || S.isEmpty()) {
            return TheList;
        }

        String[] StringArray = S.split(D);

        if (StringArray == null || StringArray.length==0) {
            return TheList;
        }

        return new ArrayList(Arrays.asList(StringArray));
    }
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #6  
Old 02-07-2011, 06:48 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Quote:
Originally Posted by tmiranda View Post
When I see warnings I assume I am doing something that I should not be doing.
Or not doing something you should be doing. In this case you want to narrow the generic ArrayList constructor to return a List<String>. Try this:

Code:
return new ArrayList<String>(Arrays.asList(StringArray));
__________________
-- Greg
Reply With Quote
  #7  
Old 02-07-2011, 07:17 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
That made it compile cleanly, thanks Greg.
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Playback Exception KipKasper SageTV Software 6 11-29-2010 06:15 PM
Exception errors bits Batch Metadata Tools 4 07-04-2010 09:19 PM
HDhomeRun playback exception mattbrad2 Hardware Support 3 12-15-2006 09:05 AM
Stumped at java exception beelzerob SageTV Studio 12 07-25-2006 11:46 PM
Java Exception mike1961 SageTV Software 0 12-20-2005 06:13 PM


All times are GMT -6. The time now is 06:04 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.