Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: override list add method?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default override list add method?

    In the code below, StringUtils is a custom object we have to do certain operations. All I'm trying to do is take a delimited string, split it up into a list, add a new element to the List before dumping it back into a delimited string.

    List<String> reportList = StringUtils.delimSplitToList(reports, "|");
    reportList.add(repID + "-0");				
    reports = StringUtils.join(reportList, "|");

    I'm getting an exception:
    null
    java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:151)
    at java.util.AbstractList.add(AbstractList.java:89)
    What I've read is that the add method needs to be overridden. How do I do that? Or is there a better way to do what I want to accomplish?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: override list add method?

    null
    java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:151)
    What does the printout of null mean?
    Where is the rest of the stack trace showing what line in your code did the call?
    What does that code look like?

    You'll have to post more code than you have for anyone to help.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: override list add method?

    I didn't want to expose my architecture by posting the full stack trace. And the the stack trace indicates that the middle line : reportList.add(repID + "-0"); is causing the exception.

    I'm not sure what else to post that would provide any more value than that.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: override list add method?

    As Norm pointed out, since StringUtils is a custom class we have no clue what it returns or takes as arguments for those methods. We can guess based upon the behavior - it appears to return a List which does not have the add method implemented. A quick workaround would be to add the returned List to a newly made ArrayList, add whatever you need to that ArrayList, then pass the ArrayList to the join method

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: override list add method?

    Yeah, you're right. Sorry about that. I am just trying to be careful, maybe too careful.

    I think I may have been trying to do an add on a null value, which could have been causing the exception but I think your other suggestion may also work if that doesn't turn out to be the case.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: override list add method?

    How can calling the List class's add() method with a String possibility cause an error?
    I assume that repID + "-0" is a String.
    provide any more value than that
    That provides no value at all. None.

    The null printed out should tell you something.

  7. #7
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: override list add method?

    repID is a string and I'm just concatenating it to a string, so yes, it would be a string.

    And your question is the entire reason I posted here because I was thinking the same thing.

    I think that the null was possibly because reportList was null. I'm still working on that theory but I have another problem now that I moved some things around. I'll post again if I still run into a problem.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: override list add method?

    Comment about printing out values:
    Add an id, like the name of the variable, to the println to show exactly where it came from:
    println("var=" + var);
    if there are more than one:
    println("1var=" + var);
    ....
    println("2var=" + var);

  9. #9
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: override list add method?

    I do appreciate the fast responses and the help even if I'm not very forthcoming with information! I'll do better in the future.

  10. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: override list add method?

    Quote Originally Posted by ober0330 View Post
    All I'm trying to do is take a delimited string, split it up into a list, add a new element to the List before dumping it back into a delimited string.
    If that is really the requirement, why not just concatenate a new element to the delimited string directly? e.g.
    String original = getDelimitedString();  // whatever
    String result = original + "|" + newElement;
    If you have to use a List for some reason, you don't need a special method to separate your string, you can use String.split(..), e.g:
    String test = "foo|bar|paf";
    List<String> list = new ArrayList<String>();  // mutable destination list
    list.addAll(Arrays.asList(test.split("\\|")));
    list.add("spod");
    ...

  11. The Following User Says Thank You to dlorde For This Useful Post:

    ober0330 (July 17th, 2011)

  12. #11
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: override list add method?

    I was loading it into a list because I have to loop through the elements and remove/update the list if the item I'm currently processing is already there.

    I might use your second option. Thank you.

Similar Threads

  1. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  2. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  3. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM
  4. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM