This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
suppose I have 5 items. Inside the 1st for loop, the code that prints out the name of each item seems to be fine, but inside the 2nd for loop, it will print only the last element 5 times. In other words, the vector seems to "reset" each time and the value I get returned is a vector with the last value stored from the for loop, and not a vector of different values.
I can think of one way that this would occur. What does your getItemByID method look like? If it, for some reason, returns the same item but with different values, then I would expect these results. Try editing your code as such:
Of course, if you've overridden .toString(), comment out that override for testing! If you discover that you are adding the same object again and again, then you will need to change how the getItemByID method works. Other than that, I can't think of anything else that would cause something like this. Certainly the problem is not in the section of code. And, outdated as Vectors are (use Lists instead), I don't think that they've developed any senility problems .
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Strange - perhaps the problem is in the code for getItemByID(). It looks like maybe you're always returning the same Item object, but just changing its name each time? (In the first loop that is.) Try inserting this into both loops: System.out.println(System.identityHashCode(x)); This wil show if it's really the same object all 5 times. If it is, look at getItemByID() to fix it.
"I'm not back." - Bill Harding, Twister
Joined: Jun 19, 2002
Joel, and Jim, Thanks for youe answers. I will definately look into it more closely. but just for clarity, I have included the getItemByID methos below
This method is suppose to return an item object given the ID of the item. The two dimensional array is filled from a while loop for resultset object and the capital letters are constants for clarity. I didn't seem to get problems with this method, cause I tried it numerous times, but when I call it from getCatalog method, it fails. [ June 28, 2003: Message edited by: Amir Ghahrai ] [ June 28, 2003: Message edited by: Amir Ghahrai ]
Joined: Jan 30, 2000
Why do you call new Item() twice in this method? It looks like you find the product for a given ID, create a new Item using data from that product - then you create another new Item which has nothing to do with the first, and return the new Item rather than the old one. The fact that getName() seems to return different values despite the errors in getItemByID() suggests that getName() is accessing data stored somewhere other than the Item instance. Is name a static variable perhaps? I didn't seem to get problems with this method, cause I tried it numerous times, but when I call it from getCatalog method, it fails. I suspect that you have not tested this as carefully as you think. I think your Item only remembers the most recent data that was referenced - instances are not remembering the old data. At least, the name is not being remembered correctly. This is just the first time you've looked carefully at any instance other than the one you just created. That's my guess, anyway. Good luck.
Joined: Jun 19, 2002
Ok, the problem was not in the getItemByID method but in the Item class. In the Item class, I had declared the variables as static and ofcourse each time, the methods were calling on the same value as before, and not instantiating Item objects with different values. (Ofcourse it took me 3 days to notice this!)