• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector not keeping values

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)
 
reply
    Bookmark Topic Watch Topic
  • New Topic