jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Trying to add string values to a list or some type of collection through iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trying to add string values to a list or some type of collection through iterator" Watch "Trying to add string values to a list or some type of collection through iterator" New topic

Trying to add string values to a list or some type of collection through iterator

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

Here is the iterator:

When i run that it prints out the deviceId so the String x is definitely holding the value I am needing it to. But now I need to figure out how to add those values to a list. There could be anywhere from 0-100 values. I tried something like this:

But there is a mismatch between String and list there, because device.getDeviceId returns a String. Is there a quick method to do this parsing? I mean Ive learned the basic things most students learn in a java class for converting from say int to String, but we did not really eal with lists. Can anyone give me a tip on this, Im sure it is something easy

edit - in the mean time I am going to look at some more java list tutorials

True wisdom is in knowing you know nothing - Socrates
Henry Wong

Joined: Sep 28, 2004
Posts: 20517

If you want to store the strings in a list, then you simply create that list and add it... Declaring the string as a list, and "parsing" that string to a list makes no sense.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

Maybe you can help clear up what I should do here, I know I am close but not quite there.

I have called data froma web service, and used simplexml to parse that data. Then I set this inputStream equal to deviceColelction.
So now deviceColelction holds this input stream and when I tried to print out the value of deviceId from deviceCOllection I got the value I thought I would get.
The code to do so looked like:

So now for every device in my deviceCollection I am able to get the value of DeviceId. Remember these values are originally stored in xml form like this:

Now it took me DAYS to just get it to this point. But I am now able to iterate through these values, and then assign them to a String value. Now the final step is to use the values that this returns to populate the branches of a rifchfaces tree. My only options for this value are collection(list), array or map, because those are the only data types that a recursive tree node adaptor accepts, according to the people who wrote the richfaces code.

So I need to somehow iterate through my deviceCollection (which is storing that xml as an input stream), and have it pull out whatever is between the <deviceId> tag, which I seemingly have the code for up to this point. Then I need to add this value to one of those three data types, and add a getter for it.

I know I have to be close, because if Im no Im going to go postal. But it seems I am... Like I said, I can go through my deviceColelction after it has taken the data from the web service, and get it to return the value I am looking for. Now I just need to add this value to one of those three data types, so I can make a getter for it and bind that to the recursive tree node on my jsf page.

What would be your advice here?

edit - and unfortunately it is not an option to change device.getDeviceId to something other than String value. It would just ruin too much other code that is working already, essentially i would have to trash a good month of work
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

I guess the easiest way to say it would be that i need a collection of some sort to hold the values returned from:

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

eh forget it, I have been at this for too long and made too little progress. I can get a tree to show up using a .properties file as my data source, but that is the best I have been able to do.
It sucks, and it wont look good, but I am just gonna have to tell this company that this is out of the range on my abilities.
I will get this project as far as I can using a .properties file, and that is just gonna have to be it. What the hell else can I do, maybe I have chosen the wrong career path
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

If that's all you want to do -- and in your other thread, I asked if that's what you wanted to do, and the conversation that started up with the other guy seemed to indicate you wanted something much more complex -- then it's very very easy. Create the new list before the loop, and then inside the loop, use "add()" to add each one to the list.

[Jess in Action][AskingGoodQuestions]
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

Well, although that did work and I now have a list ids that is holding the values, when I bind it to my tree it just comes up as a blank page.
Thanks for the help.
I hate to quit but at some point you have to ask yourself, "do I keep working on this one little thing and risk having nothing to show by the deadline, or move on so I at least have a few other things to show?"

I decided to make one last post to try and get this solved. If someone is up for a challeneg here it is:


Dont even click the link if you dont want to spend some time trying to solve this, it will just be a waste of your time because I highly doubt someone will be able to just read through the post and see what my problem is. Youll probably have to copy and try some of the code yourself to solve this one. Just a warning so no one wastes their time
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

thought some of you who have seen me working on thsi same problem for two weeks might like to know it turns out my source code is all working fine, it has something to do with the JARs...
someone on the rich forums finally got tired of seeing all my possts Im sure, because he asked me to upload my source code so he could check it himself...
here is what he posted:

I've removed throws (wrapped all in try/catch for now) and successfully launched the application on Tomcat 6 and JBoss 5.0.1.GA (removed jsf-api/jsf-impl and stax-* jars from the project before deploying to JBoss). I see one leaf tree - as in the source XML file.

Of course I tried his fix, but I still have problems (big surprise eh)
Here is my reply to his post:

I messed things up for myself even more now it would seem.
How do you start the server without the jsf api and jsf impl jars, every time I try to I get this log:

SEVERE: Exception starting filter richfaces
java.lang.NoClassDefFoundError: javax/faces/FacesException
at org.ajax4jsf.webapp.BaseFilter.init(BaseFilter.java:170)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:627)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.ClassNotFoundException: javax.faces.FacesException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 28 more

And then when I add those jars back in the server starts with no errors, but then when trying to launch the tree I get

javax.servlet.ServletException: java.lang.reflect.InvocationTargetException

This is even more frustrating to know that my methods are all working but it is my friggin libraries messing things up!

And you didnt edit the web.xml or faces-config files at all then?

So now Im pretty much back where I started, just with a better idea of what the problem is, but even less clue about how to fix it. At least with java code I have some tidbit of experience...
Martijn Verburg

Joined: Jun 24, 2003
Posts: 3275

Hi Matt,

Tomcat/Jboss already come with those libraries, hence you should not need to bundle them. What application server are you running on? Can you see their library that implements the spec?

Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

edit - I created a new project where I removed all the redundancies, and used server supplied implementation rather than mojarra (jsf-api and jsf-impl), but I get errors starting the server. But as soon as I put jsf-api and impl into my lib folder the server starts fine. So Im being told those jars are causing my problems, yet I cant get ther server started without them!

Im not sure what your referring to by application server. This setting up of JARs s totally new territory to me, it was only by some miracle that I got things working in the first place. Here is the only way in which I can get my server to start without errors, by having these libraries:

Apache Commons

JBoss v5.0
waaay to numerous to list

JRE System Library - this libraray just shows up without me specifying it

Mojarra (is implementation)

RFJars (I created this one)

RichFaces (another I created)

WebApp (jars show up in here after I add libraries to WEB-INF/lib)

plus two free jars that are under no library heading

So I have doubles of commons-beanutils, commons-collections, and commons-logging; and triples of commons-digester. Before i tried the fix I got from the rich forum I only had one copy each of jsf-api and jsf-impl in the mojarra library, but now the only way to get my server to start correctly is by also placing these two jars in WEB-INF/lib, creating duplicates of them. However if Im understanding I should not need those two at all. But if that is o, why does my serve get errors every time I try to start without them (unless there are no projects deployed to the server)

I should give the steps I take when creating a new project to I think.

New->dynamic web project->
target runtime is jboss5.0
configuration is facelet 1.0 project
then when asked to choose between server suplied implementation or one of my choosing, I choose my own - mojarra (which includes jsf-api and jsf-impl jars only)
then Im also prompted to choose other libraries and I choose
-apache commons (commons-beanutils, commons-collections, commons-digester, commons-logging)
-RFJARS which is one I created (commons-beanutils, commons-collections, commons-digester, commons-logging, jsf-facelets, jstl-1.2)
-Richfaces, another library I created - (richfaces-api, richfaces-impl, richfaces-ui)
then hit finish. after that I still needed some other jars so I manually added them to the build path. they are (commons-codec, and yet another commons-digester)
and finally I put some jars in my web-inf/lib folder (simple-xml, stax-1.2.0, and stax-api-1.0.1) Ive recently also had to add jsf-api and jsf-impl to this folder because after I tried to remove the mojarra libraray upon advice from the jboss forums, now the only way to get my server started correctly is to have those to jars in this folder

I think these lines from my web.xml file may be the problm:

As soon as I removed those though my libraries started going crazy and changing positions and eclipse wont let me do anything... we shall see...
Martijn Verburg

Joined: Jun 24, 2003
Posts: 3275

Hi Matt,

Hmm, not sure if the advice that person gave you is correct in that case! I'm not sure what else you can try to be honest.

Your <servlet> configuration is fine however, it's just what loads on the startup of your webapp.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

Ot was from nbevalaeski, one of the people who wrote a lot of the rich code I believe. At least his name is on a lot of it.

I just packaged my deployment here:


and he downloaded it and said he was able to run it just fine after changing the jars.

Here is the thread where all this happened


If you feel like looking into any of this stuff
I agree. Here's the link: http://aspose.com/file-tools
subject: Trying to add string values to a list or some type of collection through iterator
It's not a secret anymore!