Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Overriding existing Serializeable writeObject ?

Reply
Thread Tools

Overriding existing Serializeable writeObject ?

 
 
jprogrammer
Guest
Posts: n/a
 
      04-20-2004
I have a rendering class that extends AWT Panel for drawing purposes. I
made this class Serializable so that I can send it over the socket. The
problem I found out is that my implementation of writeObject () and
readObject() for my rendering frame is completely ignored!! Panel class's
default writeObject, readObject is invoked instead. I tested this using
println(). How do I fix this? Here's some code out of my class:

public class DrawFrame extends Panel implements Serializable,MouseListener,
MouseMotionListener{


public int side1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
public int side2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
public int side3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
public int side4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
public int side5[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
public int side6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

//------------------
private void writeObject(ObjectOutputStream out)
{
try{
for(int i=0;i<9;i++)
{
out.writeInt(side1[i]);
out.writeInt(side2[i]);
out.writeInt(side3[i]);
out.writeInt(side4[i]);
out.writeInt(side5[i]);
out.writeInt(side6[i]);
}

}
catch(IOException e)
{
//System.out.println(e);
}

}
private void readObject(ObjectInputStream in)
{
try{
for(int i=0;i<9;i++)
{
side1[i]=in.readInt();
side2[i]=in.readInt();
side3[i]=in.readInt();
side4[i]=in.readInt();
side5[i]=in.readInt();
side6[i]=in.readInt();
}
}
//catch(ClassNotFoundException e){}
catch(IOException e){}

}


 
Reply With Quote
 
 
 
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      04-20-2004
On Tue, 20 Apr 2004 09:37:10 -0400, jprogrammer wrote:

> I have a rendering class that extends AWT Panel for drawing purposes. I
> made this class Serializable so that I can send it over the socket. The
> problem I found out is that my implementation of writeObject () and
> readObject() for my rendering frame is completely ignored!! Panel class's
> default writeObject, readObject is invoked instead. I tested this using
> println(). How do I fix this? Here's some code out of my class:


[...]

> private void writeObject(ObjectOutputStream out)


[...]
> private void readObject(ObjectInputStream in)


You didn't read the API or didn't understand it:

Classes that require special handling during the
serialization and deserialization process must
implement special methods with these exact signatures:

private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;

Your signatures miss the throws-parts.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      04-20-2004
jprogrammer wrote:
> I have a rendering class that extends AWT Panel for drawing purposes. I
> made this class Serializable so that I can send it over the socket. The
> problem I found out is that my implementation of writeObject () and
> readObject() for my rendering frame is completely ignored!! Panel class's
> default writeObject, readObject is invoked instead. I tested this using
> println(). How do I fix this?

<snip>

The javadocs specify:
"Classes that require special handling during the serialization and
deserialization process must implement special methods with these exact
signatures:

private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;"

Your classes don't throw the required exceptions and so don't match
the signatures. The javadocs should always be your first point of
reference...

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-20-2004
On Tue, 20 Apr 2004 09:37:10 -0400, "jprogrammer" <(E-Mail Removed)> wrote
or quoted :

> The
>problem I found out is that my implementation of writeObject () and
>readObject() for my rendering frame is completely ignored!!


see http://mindprod.com/jgloss/serialization.

Did you leave out an "implements" anywhere?

Do your method signatures exactly match?


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-21-2004
Oh I thought the methods "threw" the exceptions and i was supposed to catch
them. I guess i completely misunderstood. Now, do i have to throw
exceptions in any particular order?

"Lothar Kimmeringer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 20 Apr 2004 09:37:10 -0400, jprogrammer wrote:
>
> > I have a rendering class that extends AWT Panel for drawing purposes. I
> > made this class Serializable so that I can send it over the socket. The
> > problem I found out is that my implementation of writeObject () and
> > readObject() for my rendering frame is completely ignored!! Panel

class's
> > default writeObject, readObject is invoked instead. I tested this using
> > println(). How do I fix this? Here's some code out of my class:

>
> [...]
>
> > private void writeObject(ObjectOutputStream out)

>
> [...]
> > private void readObject(ObjectInputStream in)

>
> You didn't read the API or didn't understand it:
>
> Classes that require special handling during the
> serialization and deserialization process must
> implement special methods with these exact signatures:
>
> private void writeObject(java.io.ObjectOutputStream out)
> throws IOException
> private void readObject(java.io.ObjectInputStream in)
> throws IOException, ClassNotFoundException;
>
> Your signatures miss the throws-parts.
>
>
> Regards, Lothar
> --
> Lothar Kimmeringer E-Mail: (E-Mail Removed)
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-21-2004
On Tue, 20 Apr 2004 22:55:39 -0400, "Paul" <(E-Mail Removed)> wrote
or quoted :

>Oh I thought the methods "threw" the exceptions and i was supposed to catch
>them. I guess i completely misunderstood. Now, do i have to throw
>exceptions in any particular order?


No.
See http://mindprod.com/jgloss/serialisation.html
See http://mindprod.com/jgloss/serialisation.html
See http://mindprod.com/jgloss/serialisation.html
See http://mindprod.com/jgloss/serialisation.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      04-21-2004
> Your classes don't throw the required exceptions and so don't match
> the signatures. The javadocs should always be your first point of
> reference...
>



A method signature is strictly defined by its name and (order and type) of
its parameters.
The declared exceptions are NOT part of the method signature.
Another misconception is that the return type of a method is part of its
signature, which is incorrect.

// same signature
void m(int i, String s)
int m(int i, String x) throws java.io.IOException
String m(int x, String t) throws RuntimeException

// different signature to those above
void m(String s, int i) throws java.io.Exception
void m(int i)
void mm(int i, String s)

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      04-22-2004
On Tue, 20 Apr 2004 22:55:39 -0400, Paul wrote:

> Oh I thought the methods "threw" the exceptions and i was supposed to catch
> them. I guess i completely misunderstood. Now, do i have to throw
> exceptions in any particular order?


No, just put away the try-catch-blocks and add the throws-
Statement in the signature of your method. You should catch
the exceptions where you _use_ the serialization of the
object.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      04-22-2004
On Wed, 21 Apr 2004 16:56:52 +1000, Tony Morris wrote:

>> Your classes don't throw the required exceptions and so don't match
>> the signatures. The javadocs should always be your first point of
>> reference...
>>

>
> A method signature is strictly defined by its name and (order and type) of
> its parameters.


In this case it was meant that way, that
you have to declare the method exactly as being described
in the Javadocs

> The declared exceptions are NOT part of the method signature.
> Another misconception is that the return type of a method is part of its
> signature, which is incorrect.


I can try that later, but AFAIR the compiler don't accept
methods with the same name and parameters but different
return-types and you get compile-errors as well when you
leave away or add exceptions to methods that are declared
in superclasses.

Google tells me I'm right and Patrick Niemeyer writes the
same in his book "Exploring Java":

A method signature is a collection of information about
the method, as in a C prototype or a forward function
declaration in other languages. It includes the method's
name, type, and visibility, as well as its arguments
and return type.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      04-22-2004
On Thu, 22 Apr 2004 07:50:51 +0200, Lothar Kimmeringer wrote:

> On Wed, 21 Apr 2004 16:56:52 +1000, Tony Morris wrote:

....
>> A method signature is strictly defined by its name and (order and type) of
>> its parameters.

....
>> The declared exceptions are NOT part of the method signature.


I was skeptical so I went on a long hunt
at Sun to try and nail it down.. I saw one
reference that suggested Tony was right,
but it did not seem definitive to me.

....
> I can try that later, but AFAIR the compiler don't accept
> methods with the same name and parameters but different
> return-types and you get compile-errors as well when you
> leave away or add exceptions to methods that are declared
> in superclasses.


Then I thought to toss that and try it..
<sscce>
import java.io.IOException;

public interface ImplementThis {
public abstract String getTheStuff() throws IOException;
}

class Implementor implements ImplementThis {
public String getTheStuff() {
return "";
}
}
</sscce>

Compiles just fine..

So, no - I now have to concede I have
no idea why your code is failing, maybe
we need to go to help level 2..
<http://www.physci.org/codes/sscce.jsp>

And note that URL does not refer to
'some code' from your class, but a self
contained example that fails.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writeObject signature Roedy Green Java 35 04-15-2006 04:10 AM
writeObject and readObject problem juicy Java 28 09-25-2004 02:43 PM
Socket / writeObject / NotSerializableException question JMC Java 1 04-07-2004 01:30 PM
writeObject too slow for big object, any idea? Kaidi Java 7 01-06-2004 11:32 AM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM



Advertisments