Problem with collecting several classes in a Vector.

Hi,

sorry if I'm asking about a thing which was discussed earlier, but with the search tool I found nothing :-((

I have a following problem: I got some data from the DB. First, it will be packed in a class (say, it's called myClass) which has set-methods for that. Then this class is packed into a vector and the same thing repeats as long as the ResultSet has more data. It looks like this:

ResultSet rs = myPreparedStatement.executeQuery();

while(rs.next()){

myClass = new MyClass();

myClass.setVar1(rs.getInt("Column1"));

myClass.setVar1(rs.getString("Column2"));

...

myClass.setVarX(rs.getInt("ColumnX"));

vector.add(myClass);

myClass = null;

}

The problem is that after all I have a vector full with the elements, which all have the values of the last data-set.

What am I doing wrong? Please help.

Many thanks in advance

Emil

[937 byte] By [Augusta] at [2007-10-3 5:24:19]
# 1

your program is correct, but have u checked whether the resultset is giving proper results? and also check ur bean class.

see the following code which simulates your program. it is giving proper result

main : Vector v = new Vector();

VBean vB= new VBean();

vB.setX("rajesh");

v.add(vB);

vB=null;

vB= new VBean();

vB.setX("ramesh");

v.add(vB);

vB=null;

System.out.print(((VBean)v.get(0)).getX());

System.out.print(((VBean)v.get(1)).getX());

--

public class VBean {

String x;

public String getX() {

return x;

}

public void setX(String x) {

this.x = x;

}

}

venkatarajesha at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 2

Hi,

thanks for the quick answer. Yes, I checked - the ResultSet gives the proper data and I can see while debugging that the elements of the vector are overwritten with every new data set from the ResultSet :-( (moreover, it happens synchron with the call of myClass.setVar(... ...) ) It must be something very stupid, but I couldn't find it out yet ((

Augusta at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 3

> The problem is that after all I have a vector full

> with the elements, which all have the values of the

> last data-set.

The code that you posted cannot give the behavior you describe unless all the rows in the DB are the same.

Side notes:

1) Use ArrayList, not Vector, unless you have to pass that Vector to some legacy class.

2) myClass = null is pointless and does nothing useful there.

3) When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 4
variable in the myClass are static?
venkatarajesha at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 5
> variable in the myClass are static?Ooh. Good call. I didn't think of that.
jverda at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 6

> variable in the myClass are static?

Ooh. Good call. I didn't think of that.

No, they are not static, otherwise I wouldn't lose my time writting get- and set-methods.

Thank you, jverd, for the advise about "code"-tag and sorry... I didn't believe my eyes as I see the behavior I described, but the program do behave like this :-( That's why I've written, that it must be something very stupid.

>2) myClass = null is pointless and does nothing useful there.

I have noticed it already and this line is deleted.

public Properties getDataList(Properties props){

MyClass myClass = (MyClass)props.get("myclass");

/*Here I fill my PreparedStatement with the data from myClass

and execute it */

ResultSet rs = myPreparedStatement.execute();

while(rs.next()){

myClass = new MyClass();

myClass.setVar1(rs.getInt("Column1"));

myClass.setVar1(rs.getString("Column2"));

...

myClass.setVarX(rs.getInt("ColumnX"));

vector.add(myClass);

}

props.put("vector", vector);

...

return props;

}

And as I've said - while debugging I could see all the values the ResultSet deliveres (and they are not the same) and all elements of the vector being overwritten with those new values.

Augusta at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 7

> No, they are not static, otherwise I wouldn't lose my

> time writting get- and set-methods.

Static variables can just as easily have get/set mehtods, though they typically don't.

> And as I've said - while debugging I could see all

> the values the ResultSet deliveres (and they are not

> the same) and all elements of the vector being

> overwritten with those new values.

Again, this cannot happen with this code.

How are you determining that "all elmenets of the vector are beingoverwritten"?

Show the code for myClass, and show how you're seeing this overwriting.

jverda at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 8

MyClass contains private int and String variables (it is allmost 1:1 with the table I'm getting the data from) with their get- and set-methods, nothing more.

As for seeing how the elements are overwritten - I use Eclipse for developing and there is a show "Variables" where one could see the values of every variable at every step of debugging.

Augusta at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 9
Use good old-fashioned System.out.println to print the contents of the vector. If those variables are not static, and if thse DB rows contain different data, then it is impossible for the vector to contain the same data in every element with the code you showed.
jverda at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 10

>Use good old-fashioned System.out.println to print the contents of the vector.

I did. It showed the same as the "Variables"-show of Eclipse. With every new data set delivered by ResultSet all the elements of the Vector had got new values.

>If those variables are not static, and if thse DB rows contain different data, then it is >impossible for the vector to contain the same data in every element with the code you >showed.

They are not static and the rows in the DB do contain different data.

Well, I actually found a workaround, but I realy would be interested in reasons of this behavior just to avoid it in the future. If I find out what the problem was - I'll post it here.

Many thanks for the answers.

Augusta at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 11

As I stated: The behavior you described cannot happen with the code you posted and the situation you described. Either you're interpreting something wrong or that's not the code you're running. Without seeing your code and output, I can't help you.

The only other thing I can think of is if this vector is your own class, not java.util.Vector, and you've got a bug in it.

jverda at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 12

Hello

I get the really same behavior here on my WinXP SP2 machine with Java 1.5.0_09-b03.

==========

My class is:

==========

package org.geodb;

public class Library

{

private byte[] libraryName = new byte[8];

public Library()

{

}

public void setLibraryName(byte[] v)

{

libraryName = v;

}

public String getLibraryName()

{

return new String(libraryName);

}

}

==========

My test is:

==========

import java.io.*;

import java.util.*;

import org.geodb.Library;

class Test1

{

public static void main(String args[])

{

byte[] a1 = new byte[] {12,11,10};

byte[] a2 = new byte[] {9,8,7};

byte[] a3 = new byte[] {6,5,4};

Vector<Library> v = new Vector<Library>();

Library gl = new Library();

gl.setLibraryName(a1);

v.addElement(gl);

gl.setLibraryName(a2);

v.addElement(gl);

gl.setLibraryName(a3);

v.addElement(gl);

System.out.println(v.elementAt(0).getLibraryName());

System.out.println(v.elementAt(1).getLibraryName());

System.out.println(v.elementAt(2).getLibraryName());

}

}

==========

My output is three times the same:

==========

jih

jih

jih

==========

Hmm... It's really not the first time I programm with java.

Any ideas ?

Andreas

bolle732a at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 13

Sorry, the example works when I use the following code:

Vector<GeodeLibrary> v = new Vector<GeodeLibrary>();

GeodeLibrary gl = new GeodeLibrary();

gl.setGeodeLibraryName(a1);

v.addElement(gl);

gl = new GeodeLibrary();

gl.setGeodeLibraryName(a2);

v.addElement(gl);

gl = new GeodeLibrary();

gl.setGeodeLibraryName(a3);

v.addElement(gl);

But in my real implementation it doesn't work. And there I did the new instantiating of my class.

Library gl = null;

for(int i = 0; i < applicationLibraryCount; i++)

{

gl = new Library();

dis.read(t, 0, 8);

gl.setLibraryName(t);

gl.setLibraryType(reverseBytes(dis.readUnsignedShort()));

gl.setLibraryProtocolVersionMajor(reverseBytes(dis.readUnsignedShort()));

gl.setLibraryProtocolVersionMinor(reverseBytes(dis.readUnsignedShort()));

libraries.addElement(gl);

System.out.println();

System.out.println(i + ": " + gl.getLibraryName());

System.out.println(i + " first: " + libraries.firstElement().getLibraryName());

System.out.println(i + " last : " + libraries.lastElement().getLibraryName());

}

I have been too fast with posting. Sorry again about that.

Andreas

bolle732a at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 14

OK, I did some more research and I think I got the problem.

I have a class with a member. I will call the class TestClass and the member testMember. For the member, I have a setter and a getter method called setTest() and getTest().

I fill a vector with several instances of the class TestClass with different values for the member testMember.

Now, if the member testMember is of type byte[8] and I read the values of the different instances, I get always the same value for testMember. It's the value of the last added instance. If the type of the member testMember is for example String, all is working fine.

There seems to be a bug when using a member of type byte array in this case.

It was a really confusing thing. For me, I can do the workaround of not using the byte array. But it makes my code really ugly and I hope for a fine solution to this.

Where should I send this report ? I can provide more information if needed.

Andreas

bolle732a at 2007-7-14 23:31:27 > top of Java-index,Core,Core APIs...
# 15

Can you post your latest code?

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

It's highly unlikely that you've found a bug in the API.

jverda at 2007-7-21 11:01:59 > top of Java-index,Core,Core APIs...
# 16

@Jeff:

Interesting that byte[] just crept in to the discussion:

byte[] buf = new byte[8];

loop {

is.read(b);

new MyThing(b);

v.add(myThing);

}

Aliasing?

dannyyatesa at 2007-7-21 11:01:59 > top of Java-index,Core,Core APIs...
# 17

> @Jeff:

>

> Interesting that byte[] just crept in to the

> discussion:

> >byte[] buf = new byte[8];

> loop {

>is.read(b);

> new MyThing(b);

>v.add(myThing);

>

>

> Aliasing?

Not following your point. And I haven't looked closely enough at the OP's problem to undestand it.

I'll take a wild stab and assume that he's doing something like your code, and then wonders why all the MyThings in the Vector (Why do people still even use Vector? Who teaches this?) have the same byte array.

If this is the case, the reason is of course because there is only one byte array ever created, and that code just keeps overwriting its contents and creating new references that point to it.

jverda at 2007-7-21 11:01:59 > top of Java-index,Core,Core APIs...