ObjectOutputStream

Hi frends, i've a problem....i have to send one Vector that includes two Vector and one int value.

I send this Vector with the ObjectOutputStream out

publicvoid send(Object a){

try{

out.writeObject(a);

}catch (IOException ex){

ex.printStackTrace();

}

}

but if i do this operation, i receive always the first Vector and it never changes!!

Can you help me?....please i have to deliver the project tomorrow!!

Thanks

[779 byte] By [marlborinoa] at [2007-11-26 20:08:29]
# 1

If you do this:

send(v);

...

v.add(somethingElse); //for example

send(v);

The result of the send call is not what you expect.

On the other end of the object stream, you will discover this:

Object v1 = in.readObject(); //matching first call to send(v);

Object v2 = in.readObject(); //matching second call to send(v)

if (v1 == v2) //true!!

That's the way object streams work, they preserve reference graphs.

To get around this:

1. You can create a new vector each time (and consider its contents, too!), or

2. Call method [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html#reset()]reset[/url] on the object output stream. This may not be what you

want in some cases --

you may want to preserve the reference graph -- so think this over.

DrLaszloJamfa at 2007-7-9 23:11:12 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for the assistance....but now i've another problem.

I have to send one object which belong to one class created from me....if i send it with the function out.write(object_of_my_class)

(obviusly out is one ObjectOutputStream) the program makes one errorof the kind .NotSerializableException....how i can solve this problem....

Thamks

marlborinoa at 2007-7-9 23:11:12 > top of Java-index,Java Essentials,Java Programming...
# 3
Read this: http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.htmlImplementing that interface does not mean writing "implements Serializable" suffices.
CeciNEstPasUnProgrammeura at 2007-7-9 23:11:12 > top of Java-index,Java Essentials,Java Programming...
# 4
Your class has to implement the marker interface Serializablekind regards,Jos
JosAHa at 2007-7-9 23:11:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> the program makes one

> errorof the kind .NotSerializableException....how i

> can solve this problem....

> Thamks

Make the object serializable (implementing the Serializable or Externalizable interfaces)

(oops too slow)

Message was edited by:

Ruly-o_O

Ruly-o_Oa at 2007-7-9 23:11:13 > top of Java-index,Java Essentials,Java Programming...