aspose file tools*
The moose likes Java in General and the fly likes Vector not keeping values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector not keeping values" Watch "Vector not keeping values" New topic
Author

Vector not keeping values

Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
I have a method similar to the one shown below

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.

Any suggestions to fix this?


Amir
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
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 ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
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!)
 
 
subject: Vector not keeping values