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.
Code :
List<String> reportList = StringUtils.delimSplitToList(reports, "|");
reportList.add(repID + "-0");
reports = StringUtils.join(reportList, "|");
I'm getting an exception:
Quote:
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?
Re: override list add method?
Quote:
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.
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.
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
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.
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.
Quote:
provide any more value than that
That provides no value at all. None.
The null printed out should tell you something.
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.
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);
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.
Re: override list add method?
Quote:
Originally Posted by
ober0330
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.
Code java:
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:
Code java:
String test = "foo|bar|paf";
List<String> list = new ArrayList<String>(); // mutable destination list
list.addAll(Arrays.asList(test.split("\\|")));
list.add("spod");
...
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.