1

I'm getting the following notSerializableException even though my Hub class implements serializable. This happens when I try to write to a file using ObjectOutputStream. My project contains too many classes to post here reasonably, hence I'm mostly interested in what other reasons there could be for this error other than the class not implementing Serializable. Another puzzling thing is that I am not writing an object of the Hub class to the file, I'm writing an ArrayList of objects from a completely different class which also implements serializable.

java.io.NotSerializableException: bikescheme.Hub$1
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at bikescheme.Database.updateStations(Database.java:366)
at bikescheme.Database.addStation(Database.java:235)
at bikescheme.Hub.addDStation(Hub.java:148)
at bikescheme.HubTerminal.addDStation(HubTerminal.java:71)
at bikescheme.HubTerminal.receiveEvent(HubTerminal.java:52)
at bikescheme.EventDistributor.sendEvent(EventDistributor.java:75)
at bikescheme.EventDistributor.sendEvents(EventDistributor.java:98)
at bikescheme.SystemTest.runTestAndReturnResults(SystemTest.java:359)
at bikescheme.SystemTest.runAndCheck(SystemTest.java:341)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
3
  • Does your Hub class happen to have reference type member variables that are not serializable? Commented Nov 23, 2015 at 0:15
  • 3
    The class bikescheme.Hub$1 is not serializable. That should be the first anonymous inner class in Hub.java . Commented Nov 23, 2015 at 0:15
  • Is it possible to post all the code for bikescheme.Hub? We may be able to show you where the anonymous class is. Commented Nov 23, 2015 at 0:27

3 Answers 3

3

Looks like you have an anonymous inner class object that isn't serializable (bikscheme.Hub$1):

java.io.NotSerializableException: bikescheme.Hub$1

Sign up to request clarification or add additional context in comments.

7 Comments

What exactly is an anonymous inner class? I have an instance of a singleton class declared however it also implements serializable.
@mankee if you don't know what an inner class is, then I think you need to read more of the Java basics before tackling serialization.
I know what an inner class is, I never dealt with an anonymous class tho
Alright I found the anonymous class, how do I make it implement Serializable though, I already put implements Serializable in its definition as well as in the definition of all private inner classes it has.
@mankee I think you need to show us all the code for bikescheme.Hub.
|
2

This should answer all the questions:

public class BadSerializingClass implements Serializable {

    class ThisIsNotOK {} // comment out, it'll work

    class ThisIsOk implements Serializable {}

    // MVs
    ThisIsNotOK m1  = new ThisIsNotOK();    // comment out, it'll work
    ThisIsOk    m2  = new ThisIsOk();

    Runnable    innerAnonymousClass = new Runnable() { // comment out, it'll work
        @Override public void run() {
            System.out.println("Hello");
        }
    };

    public static void main(final String[] args) throws FileNotFoundException, IOException {
        final BadSerializingClass s = new BadSerializingClass();
        final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.dat"));
        oos.writeObject(s);
        System.out.println("All OK");
    }

}

4 Comments

The problem is my inner class is referenced like this ` Clock.getInstance().scheduleNotification(. It isn't actually defined inside the Hub class... I already added Clock implements serializable in the Clock.java file but it didn't help
If you cannot make it Serializable, you got two options: 1) give the variable the transient keyword => Java will not try to de/serialize it 2) use Custom Serialization: oracle.com/technetwork/articles/java/javaserial-1536170.html
I added private transient Clock clock = Clock.getInstance(); and removed the implements Serializable. Now I get the not serializable exception on bikeScheme.Clock
This should answer all the questions how? Explain please. Mere code is not sufficient.
0

Adding transient to all instances of Clock within the project has solved this problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.