By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

User-defined classes and serialization

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.