0

My code below throws java.io.NotSerializableException. Why? Thanks!

private void test3() {
   Element3 element3=new Element3();
   ObjectToBytes3(element3);
}

private class Element3 implements Serializable{
  int code=0;
}

//-----------------------------------
private void ObjectToBytes3(Element3 elem){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(elem);
        byte[] myBytes = bos.toByteArray();
    }
    catch (Exception e) {myPrint("exception",e.toString());}
    finally {
        try{
            out.flush();
            out.close();
            bos.close();
        }
        catch (Exception e) {}
    }
}

The output and stack is as follows: 2674,25 ObjToBytes: 2674,32 exception: java.io.NotSerializableException: java.util.concurrent.ThreadPoolExecutor stack= java.io.NotSerializableException: java.util.concurrent.ThreadPoolExecutor at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)


It's fun, but the code below works okay

================================
class My implements Serializable{
    byte a=1;
    int b=88;
    String s="ggg";
    private My(byte a1, int b1,String s1) {
        a=a1;
        b=b1;
        s=s1;
    }
}

private void test() {
    My my=new My((byte)5,10,"jjj");
    ObjectToBytesTest(my);
}

//-----------------------------------
private void ObjectToBytesTest(My my){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(element);
        byte[] myBytes = bos.toByteArray();
    }
    catch (Exception e) {print("exception",e.toString());}
    finally {
        try{
            out.flush();
            out.close();
            bos.close();
        }
        catch (Exception e) {}
    }
}
2
  • your code works fine. May be you have problem not in this piece of code. Show full stack trace Commented Nov 27, 2013 at 5:54
  • possible duplicate of Java Serializable Object to Byte Array Commented Nov 27, 2013 at 6:19

1 Answer 1

2

it because Element3 instance elem has a implicit reference to the outter object.

when serialize the Element3 use default action of Serializable interface, it will serialize outter object, and the outter object hasn't implement the Serializable interface,so it will throws java.io.NotSerializableException.

And the exception will refer to the class of outter object that didn't implement the Serializable interface.

in my code, it was:java.io.NotSerializableException: serializable.ObjectToBytesTest

to resolve this question, you should move the Element3 class out of the outter class,like this:

    public class ObjectToBytesTest {
      private void ObjectToBytes3() { ... }
    }

    class Element3 implements Serializable {
        int code=0;
    }

Or just as your solution(My class implement the Serializable interface),but i think this solution is not recommended.

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

1 Comment

Sorry, not understood what to do.

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.