2

这是我的代码:

class Collar {
  int size;
}

class Dog implements Serializable {
  int weight;
  transient Collar c;

  public Dog(int weight, int size) {
    this.weight = weight;
    c = new Collar();
    c.size = size;
  }

  public void writeObject(ObjectOutputStream os) throws Exception {
    os.defaultWriteObject();
    os.writeInt(c.size);
  }

  public void readObject(ObjectInputStream is) throws Exception {
    is.defaultReadObject();
    c = new Collar();
    c.size = is.readInt();
  }
}

public class test {
  public static final String FILE_REVISION = "$Revision$";

  public static void main(String ar[]) throws Exception {
    Dog dOut = new Dog(20, 2);

    System.out.println("DOut Weight: " + dOut.weight + " Size: " + dOut.c.size);

    FileOutputStream fo = new FileOutputStream("Dog.ser");
    ObjectOutputStream os = new ObjectOutputStream(fo);
    os.writeObject(dOut);

    FileInputStream fi = new FileInputStream("Dog.ser");
    ObjectInputStream is = new ObjectInputStream(fi);
    Dog dIn = (Dog) is.readObject();

    System.out.println("DIn Weight: " + dIn.weight + " Size: " + dIn.c.size);
  }
}

这是输出:

DOut Weight: 20 Size: 2
Exception in thread "main" java.lang.NullPointerException
    at test.main(test.java:55)

第 55 行是具有 System.out.println() 的最后一行代码

程序能够取回可序列化的对象,这里是 Dog。但是,当我使用自定义 readObject 方法回读时,它无法创建新的“包含”对象 Collar。我哪里错了?

如您所见,'dIn.c.size' 语句中的输出为 NullPointerException。尽管我在自定义 readObject 方法中将 c 设置为新对象,但它并没有真正起作用。

4

2 回答 2

2

因为Collar ctransient里面readObject你需要先c初始化

c.size = is.readInt();<-- NullPointerException Here

这里需要先初始化C

c = new Collar();
c.size = is.readInt();

两个方法签名都是错误的,它们应该如下所示:

 private void writeObject(ObjectOutputStream stream) throws IOException {
    stream.defaultWriteObject();
    stream.writeInt(c.size);
}

private void readObject(ObjectInputStream stream) throws IOException,
        ClassNotFoundException {
    stream.defaultReadObject();
    c = new Collar();
    c.size = stream.readInt();
}

您可以在此处阅读有关序列化的更多详细信息

于 2012-10-14T12:12:44.383 回答
0

在方法中初始化您的Collar成员readObject

public void readObject(ObjectInputStream is) throws Exception {
    is.defaultReadObject();
    c = new Collar();
    c.size = is.readInt();
}

编辑: 方法Serializable需要更改为私有。

于 2012-10-14T12:13:14.450 回答