Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting object to byte array throws NotSerializableException
    primarykey
    data
    text
    <p>My code below throws java.io.NotSerializableException. Why? Thanks!</p> <pre><code>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) {} } } </code></pre> <p>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)</p> <hr> <p>It's fun, but the code below works okay</p> <pre><code>================================ 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) {} } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload