【问题标题】:Converting object to byte array throws NotSerializableException将对象转换为字节数组会引发 NotSerializableException
【发布时间】:2013-12-12 15:24:43
【问题描述】:

我下面的代码抛出 java.io.NotSerializableException。为什么?谢谢!

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) {}
    }
}

输出和堆栈如下: 2674,25 对象字节: 2674,32 异常:java.io.NotSerializableException:java.util.concurrent.ThreadPoolExecutor stack= java.io.NotSerializableException: java.util.concurrent.ThreadPoolExecutor 在 java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) 在 java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) 在 java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) 在 java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) 在 java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) 在 java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) 在 java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) 在 java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) 在 java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) 在 java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)


这很有趣,但下面的代码可以正常工作

================================
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) {}
    }
}

【问题讨论】:

标签: java serialization


【解决方案1】:

这是因为 Element3 实例 elem 隐式引用了外部对象。

当使用Serializable接口的默认动作序列化Element3时,会序列化外部对象,而外部对象没有实现Serializable接口,所以会抛出java.io.NotSerializableException。

并且异常将引用未实现 Serializable 接口的外部对象类。

在我的代码中是:java.io.NotSerializableException: serializable.ObjectToBytesTest

要解决这个问题,您应该将 Element3 类移出外部类,如下所示:

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

    class Element3 implements Serializable {
        int code=0;
    }

或者就像你的解决方案(我的类实现了 Serializable 接口),但我认为不推荐这种解决方案。

【讨论】:

  • 抱歉,不明白该怎么做。
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2018-02-14
  • 2016-10-31
  • 1970-01-01
  • 2014-01-31
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多