Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error reading serialized objects from a file

15 views
Skip to first unread message

bill

unread,
Oct 15, 2001, 3:59:25 PM10/15/01
to

I wrote this program (say, MyApp), and everything was working fine
until I had to break things up into packages. Now when I try to
compile and run my program, I get a runtime error that seems to have
something to do with reading serialized Java objects from a file.
(This reading from a serialized objects file worked without a hitch
before I organize the software into packages.) I give a full listing
of the error below, but here are the highlights:

java.lang.ClassNotFoundException: Doodad
.
.
.
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:654)
at java.io.ObjectInputStream.inputClassDescriptor(ObjectInputStream.java:9
18)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:366)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1186)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:386)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at com.myjob.MyApp.main(MyApp.java:427)
Exception in thread "main" java.lang.IllegalArgumentException: arg is null
at com.myjob.MyModel.<init>(MyModel.java:24)
.
.
.

Things appear to start going wrong around the line:

in = new ObjectInputStream(new FileInputStream(serializedObjectsFile));
arg = (Doodad) in.readObject();


The variable serializedObjectsFile is a String denoting the name the
serialized objects file. As I said above, this reading from a
serialized objects file worked perfectly before I organize the
software into packages.

I get the same error message if I replace the first line above with:

in = new ObjectInputStream
(MyApp.class.getResourceAsStream(serializedObjectsFile));

I'm completely lost here. Help!

Thanks,

bill

Full error listing:

java.lang.ClassNotFoundException: Doodad
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:654)
at java.io.ObjectInputStream.inputClassDescriptor(ObjectInputStream.java:9
18)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:366)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1186)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:386)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at com.myjob.MyApp.main(MyApp.java:427)
Exception in thread "main" java.lang.IllegalArgumentException: root is null
at com.myjob.MyModel.<init>(MyModel.java:24)
at com.myjob.MyModel.<init>(MyModel.java:18)
at com.myjob.MyApp.makeThingie(MyApp.java:794)
at com.myjob.MyApp.makeThingiePanel(MyApp.java:1
64)
at com.myjob.MyApp.makeSubpanels(MyApp.java:113
)
at com.myjob.MyApp.<init>(MyApp.java:67)
at com.myjob.MyApp.main(MyApp.java:450)

Gordon Beaton

unread,
Oct 15, 2001, 4:08:39 PM10/15/01
to
On 15 Oct 2001 15:59:25 -0400, bill wrote:
> I wrote this program (say, MyApp), and everything was working fine
> until I had to break things up into packages. Now when I try to
> compile and run my program, I get a runtime error that seems to have
> something to do with reading serialized Java objects from a file.
> (This reading from a serialized objects file worked without a hitch
> before I organize the software into packages.)

[...]

> java.lang.ClassNotFoundException: Doodad

The missing class doesn't seem to be in any package...

Here's one theory:

Does the file contain packageless objects, i.e. that were serialized
and stored before you changed the package structure of the
application? If that's the case, then the classes stored in the file
are not the same as the classes your application (now) consists of.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
ericsson research
stockholm, sweden

Mike Schilling

unread,
Oct 15, 2001, 4:50:55 PM10/15/01
to
bill wrote:
>
> I wrote this program (say, MyApp), and everything was working fine
> until I had to break things up into packages. Now when I try to
> compile and run my program, I get a runtime error that seems to have
> something to do with reading serialized Java objects from a file.

You've already answered the question. Serialized Java objects are
identified by their fully qualified class name. If you serialize a "C1"
and try to deserialize it when class "C1" can't be found, the result is
an error even if "P1.P2.C1" can be found.

If you really need to read the serializations written under the old
class names, you'll need to create classes with the old names of your
persistent classes (e.g. "C1"). It may be possible for you to use them
only to read old serializations and have the deserialization process
convert them to instances of the newer classes: see

http://java.sun.com/products/jdk/1.2/docs/guide/serialization/spec/input.doc6.html#5903

for details about using the readReolve method to accomplish this. It
would look something like:

class C1 {
Object readResolve() {
return new P1.P2.C1(this);
}
}

bill

unread,
Oct 16, 2001, 9:47:26 AM10/16/01
to

In <9qffo7$psr$1...@news.du.uab.ericsson.se> not.fo...@see.signature (Gordon Beaton) writes:

>On 15 Oct 2001 15:59:25 -0400, bill wrote:
>> I wrote this program (say, MyApp), and everything was working fine
>> until I had to break things up into packages. Now when I try to
>> compile and run my program, I get a runtime error that seems to have
>> something to do with reading serialized Java objects from a file.
>> (This reading from a serialized objects file worked without a hitch
>> before I organize the software into packages.)

>[...]

>> java.lang.ClassNotFoundException: Doodad

>The missing class doesn't seem to be in any package...

>Here's one theory:

>Does the file contain packageless objects, i.e. that were serialized
>and stored before you changed the package structure of the
>application? If that's the case, then the classes stored in the file
>are not the same as the classes your application (now) consists of.

>/gordon


Actually, that was the first thing I tried before I posted to c.l.j.p.
I'll describe what I did, but first I should point out that the code
that generates the serialized objects file, MakeObjectsFile.java,
belongs to the default package, and imports the package to which
Doodad belongs:

import com.myjob.*;

First I re-compiled all class files using the updated source files,
and just tried to recreate the objects file:

java -classpath classes:. MakeObjectsFile inputFile objectsFile

When I ran

java -classpath classes com.myjob.MyApp objectsFile

this new objectsFile resulted in exactly the same error as the one I
posted before.

I then tried (1) replacing all references to Doodad with the fully
qualified name com.myjob.Doodad, and (2) switching MakeObjectsFile
from the default to the com.myjob package. Again, I got the same
error messages.

The error message finally changed (slightly), when I placed
objectsFile in the directory classes/com/myjob, and I invoke

java -classpath classes com.myjob.MyApp classes/com/myjob/objectsFile

but I think the change is simply because the program does not know how
to find classes/com/myjob/objectsFile. The new error message is:

java.lang.NullPointerException
at java.io.ObjectInputStream.readFullyInternal(ObjectInputStream.java:1781
)
at java.io.ObjectInputStream.bufferData(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readShort(ObjectInputStream.java:1936)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:842)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:168)
at com.myjob.MyApp.main(MyApp.java:425)


Exception in thread "main" java.lang.IllegalArgumentException: root is null
at com.myjob.MyModel.<init>(MyModel.java:24)
at com.myjob.MyModel.<init>(MyModel.java:18)

.
.
.

The part that says "root is null" suggests that the objects file was
not read at all.

bill


bill

unread,
Oct 16, 2001, 9:58:02 AM10/16/01
to

>bill wrote:
>>
>> I wrote this program (say, MyApp), and everything was working fine
>> until I had to break things up into packages. Now when I try to
>> compile and run my program, I get a runtime error that seems to have
>> something to do with reading serialized Java objects from a file.

>You've already answered the question. Serialized Java objects are
>identified by their fully qualified class name. If you serialize a "C1"
>and try to deserialize it when class "C1" can't be found, the result is
>an error even if "P1.P2.C1" can be found.

Is this true even if the code that serializes the "C1" also imports
"P1.P2.*"? (That's what my code does now.)

If so, wouldn't this mean that all the code that refers to these
serialized objects would have to change all its mentions of "C1" to
"P1.P2.C1", even when it imports "P1.P2.*"?

Thanks,

bill

Marshall Spight

unread,
Oct 16, 2001, 10:21:03 AM10/16/01
to
"bill" <bill...@hotmail.com> wrote in message
news:9qheda$pkc$1...@panix3.panix.com...

What you import has nothing to do with it. Import is a just a way
to save some typing; it doesn't change any class names.

You wrote out a class with one name, and then you renamed the
class, and then you tried to read it back in. Can't do it. If you want
to deserialize an object, the classfile for that object has to exist.
If you rename it, it doesn't exist under the old name any more.


Marshall


Cynthia Burns

unread,
Oct 16, 2001, 10:38:45 AM10/16/01
to

I don't see what you mean. As I said in another post, I re-compiled
*all* the classes after introducing the packages, including the
classes responsible for creating the serialized object file, and
re-created the object file using these new classes. The original
object files have long been out of the picture.

bill

0 new messages