all groups > vj# > may 2004 >
You're in the

vj#

group:

Serialization for Double in J#


Serialization for Double in J# Vinay Chopra
5/14/2004 5:46:02 AM
vj#: Hi

While trying to execute a code which does serialization of object i am getting following erro
"There was an error generating the XML document.

and the InnerException after debugging show
{"java.lang.Double cannot be serialized because it does not have a default public constructor."

Can anyone suggest me how to solve i

Thank
Vina
Re: Serialization for Double in J# Lars-Inge Tønnessen
5/14/2004 3:48:11 PM
Works for me. I don't know what could be wrong, you don't post your code.

public class serializeme implements java.io.Serializable
{
public java.lang.Double value;
}

/**
* Summary description for Class1.
*/
public class Class1
{
public Class1()
{
try
{
// ______------ Object ------______

// Wrire
serializeme one = new serializeme();
one.value = new java.lang.Double(4.5);
java.io.FileOutputStream file = new java.io.FileOutputStream(
"fileout" );
java.io.ObjectOutputStream output = new
java.io.ObjectOutputStream( file );
output.writeObject( one );
output.flush();
output.close();
file.close();
System.out.println( "(Written) Serialized :"+one.value );

// Read
serializeme two;
java.io.File file2 = new java.io.File( "fileout" );
java.io.FileInputStream fileinstream = new
java.io.FileInputStream( file2 );
java.io.ObjectInputStream objstream = new
java.io.ObjectInputStream( fileinstream );
two = (serializeme)objstream.readObject();
System.out.println( "(Read) Serialized :"+two.value );
objstream.close();
fileinstream.close();

// ______------ Double ------______
// Write
java.lang.Double doub = new java.lang.Double( 6.6 );
java.io.FileOutputStream file3 = new java.io.FileOutputStream(
"fileout2" );
java.io.ObjectOutputStream output3 = new
java.io.ObjectOutputStream( file3 );
output3.writeDouble( doub.doubleValue() );
output3.flush();
output3.close();
file3.close();
System.out.println( "(Written) Serialized :"+doub );

// Read
java.lang.Double doub2;
java.io.File file4 = new java.io.File( "fileout2" );
java.io.FileInputStream fileinstream4 = new
java.io.FileInputStream( file4 );
java.io.ObjectInputStream objstream4 = new
java.io.ObjectInputStream( fileinstream4 );
doub2 = new java.lang.Double( (double)objstream4.readDouble() );
System.out.println( "(Read) Serialized :"+doub2 );
objstream4.close();
fileinstream4.close();

// ______------ double ------______
// Write
double doub5 = 2.9;
java.io.FileOutputStream file5 = new java.io.FileOutputStream(
"fileout5" );
java.io.ObjectOutputStream output5 = new
java.io.ObjectOutputStream( file5 );
output5.writeDouble( doub5 );
output5.flush();
output5.close();
file5.close();
System.out.println( "(Written) Serialized :"+doub5 );

// Read
double doub6;
java.io.File file6 = new java.io.File( "fileout5" );
java.io.FileInputStream fileinstream6 = new
java.io.FileInputStream( file6 );
java.io.ObjectInputStream objstream6 = new
java.io.ObjectInputStream( fileinstream6 );
doub6 = objstream6.readDouble();
System.out.println( "(Read) Serialized :"+doub6 );
objstream6.close();
fileinstream6.close();
}
catch ( java.io.IOException ioexception )
{
ioexception.printStackTrace();
}
catch ( java.lang.ClassNotFoundException notfound )
{
notfound.printStackTrace();
}
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
new Class1();
}
}



Regards,
Lars-Inge Tønnessen
www.larsinge.com

Re: Serialization for Double in J# George Birbilis [MVP J#] [9880]
6/3/2004 5:12:21 PM
In your code you're checking yourself that the object you serialize is
java.lang.Double and convert it to "double" (a primitive type, not a
descendent of java.lang.Object if I do remember well) yourself manually
using readDouble/writeDouble (which writes "double" values, not
"java.lang.Double" object instances btw.). Suppose user wants to call
readObject/writeObject (or however it's called and pass to it a
java.lang.Double instance reference), that's the case reported

the problem seems to be the java.lang.Double class doesn't have a default
public constructor (with empty parameter list that is).

In Sun's Java too, the Double doesn't have such a constructor, that is you
can't do "Double x = new Double();" (say imply the 0 value), you have to do
"Double x = new Double(0);" or "Double x = new Double(0d);" for example.

class Double
extends java.lang.Number
implements java.lang.Comparable

but java.lang.Number is "abstract" class so it has no constructor either
(java.lang.Number extends java.lang.Object [it's implied - in the Number
class implementation it doesn't declare explicitly that it extends Object])

the question is, should Double be Serializable? What happens in Sun Java? I
think there too it can't serialize (which is bad of course). Maybe should
check Java1.5 too plus the BugParade website at java.sun.com to see if
people have been asking for this to be fixed at Sun Java (plus how many
votes it has) and if it's scheduled to be fixed or the Sun engineers replied
that's by design and won't be fixed.

The problem is either at the Serialization logic, in that it should have
special hardcoded handling maybe for the primitive-type wrapper classes of
java.lang.* (meaning Double, Integer etc. classes), or at the Double class
in that it could have an empty constructor that says:

public Double() {
this(0);
}

that is it sets the value 0 by calling the Double(double x) constructor of
the same class

cheers,
George

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com> [MVP J#] [9880]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime VCL and ActiveX controls (for PowerPoint/VB/Delphi etc.)
+ Plugs VCL and ActiveX controls (InterProcess/Internet communication)
+ TransFormations, VB6 forms to ASP.net WebForms convertion
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

[quoted text, click to view]

AddThis Social Bookmark Button