这是我的代码:
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 设置为新对象,但它并没有真正起作用。