470,862 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

User-defined classes and serialization

Hello again,

Okay, I was reading the following tutorial on serialization
of user-defined classes, and it seems pretty
straightforward:

http://www.sys-con.com/story/?storyid=44199

That being said, I can't get it to work for me. There are
no exceptions generated when I read or write to the stream,
however the class that is read back in is completely empty.
Just to be sure, here is the (comments-removed) code as I've
taken from that tutorial:

public static void SavePlayListState() throws IOException
{
ObjectOutputStream ObjectStream = null;
FileOutputStream FileStream = null;

FileStream = new
FileOutputStream(arystrLocations[2]);

ObjectStream = new ObjectOutputStream(FileStream);

ObjectStream.writeObject(MainPlayList);

ObjectStream.flush();
ObjectStream.close();

FileStream.close();
return;
}

public static void RestorePlayListState() throws
ClassNotFoundException, IOException
{
ObjectInputStream ObjectStream = null;
FileInputStream FileStream = null;

FileStream = new
FileInputStream(arystrLocations[2]);

ObjectStream = new ObjectInputStream(FileStream);

MainPlayList = (PlayList)ObjectStream.readObject();

ObjectStream.close();
FileStream.close();

return;
}

---------

The class that I'm using is a derived class of
BaseContainer, all it has added was accessor methods to
access the fields within the m_Data member.

public class BaseContainer
{
protected ArrayList m_aryContainedData;

protected BaseContainerData m_Data;

private long m_lID;

static long m_lGeneratedIDs = 0;

public BaseContainer()
{
m_lID = BaseContainer.m_lGeneratedIDs++;

m_aryContainedData = new ArrayList();
}

public BaseContainerData GetData() { return m_Data; }

public long GetID() { return m_lID; }
}

Now, from what I've read regarding these serialization
processes, I only need to write a writeExternal() method if
I don't want certain values to be written out or if the
objects within my own class are not serializable, which I
believe they are.

So, the ArrayList is declared in the base class as is the
ContainerData itself (m_Data). When I deserialize the
class, I am getting told that the ArrayList is now empty and
the strings within m_Data are all null.

I have gone over the java sdk documentation and they seem to
be telling me that what I've done should work, but obviously
I'm missing something here. Can anyone give me a nudge in
the right direction?

Thanks,
Mike
Jul 17 '05 #1
1 2170
Right well the problem was that I was writing "implements
Serializable" upon the derived class (PlayList) and not on
the base class (BaseContainer). Now it works just as it
should.

Mike

"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40***********************@lovejoy.zen.co.uk.. .
Hello again,

Okay, I was reading the following tutorial on serialization of user-defined classes, and it seems pretty
straightforward:

http://www.sys-con.com/story/?storyid=44199

That being said, I can't get it to work for me. There are
no exceptions generated when I read or write to the stream, however the class that is read back in is completely empty. Just to be sure, here is the (comments-removed) code as I've taken from that tutorial:

public static void SavePlayListState() throws IOException {
ObjectOutputStream ObjectStream = null;
FileOutputStream FileStream = null;

FileStream = new
FileOutputStream(arystrLocations[2]);

ObjectStream = new ObjectOutputStream(FileStream);
ObjectStream.writeObject(MainPlayList);

ObjectStream.flush();
ObjectStream.close();

FileStream.close();
return;
}

public static void RestorePlayListState() throws
ClassNotFoundException, IOException
{
ObjectInputStream ObjectStream = null;
FileInputStream FileStream = null;

FileStream = new
FileInputStream(arystrLocations[2]);

ObjectStream = new ObjectInputStream(FileStream);

MainPlayList = (PlayList)ObjectStream.readObject();
ObjectStream.close();
FileStream.close();

return;
}

---------

The class that I'm using is a derived class of
BaseContainer, all it has added was accessor methods to
access the fields within the m_Data member.

public class BaseContainer
{
protected ArrayList m_aryContainedData;

protected BaseContainerData m_Data;

private long m_lID;

static long m_lGeneratedIDs = 0;

public BaseContainer()
{
m_lID = BaseContainer.m_lGeneratedIDs++;

m_aryContainedData = new ArrayList();
}

public BaseContainerData GetData() { return m_Data; }

public long GetID() { return m_lID; }
}

Now, from what I've read regarding these serialization
processes, I only need to write a writeExternal() method if I don't want certain values to be written out or if the
objects within my own class are not serializable, which I
believe they are.

So, the ArrayList is declared in the base class as is the
ContainerData itself (m_Data). When I deserialize the
class, I am getting told that the ArrayList is now empty and the strings within m_Data are all null.

I have gone over the java sdk documentation and they seem to be telling me that what I've done should work, but obviously I'm missing something here. Can anyone give me a nudge in
the right direction?

Thanks,
Mike

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

60 posts views Thread by Fotios | last post: by
3 posts views Thread by zlst | last post: by
6 posts views Thread by martin | last post: by
2 posts views Thread by rn5a | last post: by
1 post views Thread by Carlettus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.