Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

[List]Update of a List in a method

Thread view: 
Daniel Moyne - 23 Dec 2006 14:42 GMT
I want to update a list transmitted as an argument and returned by the
following method:

What is wrong
public ArrayList<String> upDateExistingIndiPathClassNameValuesList(Indi
indi, ArrayList<String> classnamevalueslist) {
       Property classnameproperty=indi.getProperty(ClassNameTag);
       if (classnameproperty != null) {
               if (!classnamevalueslist.contains(classnameproperty.getValue())) {
                       //classnamevalueslist.add(classnameproperty.getValue());
                       }
               }
       }
       return classnamevalueslist;
}

butthough it does compile correctly I get an overflow error :
java.lang.StackOverflowError
       at java.io.Writer.write(Writer.java:149)
even without doing anything to this list !

Thanks.
Lew - 23 Dec 2006 17:30 GMT
> What is wrong
> public ArrayList<String> upDateExistingIndiPathClassNameValuesList(Indi
[quoted text clipped - 13 lines]
>         at java.io.Writer.write(Writer.java:149)
> even without doing anything to this list !

>> I forgot to provide the calling line :
>> ArrayList<String>myList=new ArrayList<String>();
>> myList=upDateExistingIndiPathClassNameValuesList(indi,myList);
>> where probably is the problem !
>> Thanks.

You should reply to the same thread instead of starting a new one on the same
issue.

First of all, there is no java.io in the code you posted, so the posted code
pretty certainly is not "probably ... the problem", since the error clearly
comes from a java.io.Writer. You should conclude from the error message that
code involved with a Writer is at fault, not that completely unrelated code is
involved.

Secondly, your reassignment of "myList" to the result of the method call is
redundant; it already points to the same list anyway.

Thirdly, it looks like what you need is a Set, not a List. Consequently,

fourthly, you should not name a variable ("classnamevalueslist") with its
implementation as part of the name. What sense does it make to declare

Set<String> classnamevalueslist;
?

And isn't everything a "value"? Putting "value" in the name provides no
specificity.

You should name the variable (using conventional camelCase) "classNames" or
something like that. Then you can change from Set to List to whatever without
having to rename everything.

Why a Set and not a List? Check out the Javadocs for those interfaces.

Speaking of interfaces,

fifthly, usually you should declare a variable with the interface type, and
instantiate it with the concrete type, as:

Set<String> classNames = new HashSet<String> ();

Unless you absolutely require the characteristics of a particular
implementation, this idiom provides more flexibility for plugin changes (e.g.,
to a TreeSet).

Finally, to answer your main question we would need to see the code that was
actually involved in the error. On the face of it, something is writing the
results of a recursive method that has no termination condition.

- Lew
Daniel Moyne - 25 Dec 2006 17:32 GMT
>> What is wrong
>> public ArrayList<String> upDateExistingIndiPathClassNameValuesList(Indi
[quoted text clipped - 3 lines]
>>                 if
>>                
(!classnamevalueslist.contains(classnameproperty.getValue()))
>>                 {
>>                         //classnamevalueslist.add(classnameproperty.getValue());
[quoted text clipped - 59 lines]
> writing the results of a recursive method that has no termination
> condition.
Lew,
thanks for your answer ; as a matter of fact the error was not in these
provided lines but in the ones inside the method not provided !

Now regarding my ArrayList<String> I have changed my approach to a more
global one ; as I have to work on it basically for update in more than one
method of the class I preferred to have it declared as static in the class
for those methods to access it without to mess with arguments and returned
values ; now it workd neatly.

I should have put some more thoughts on my probem before posting.
Daniel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.