gpt4 book ai didi

java - 我在 defaultWriteObject 处收到 NotActiveException,但我不知道为什么

转载 作者:行者123 更新时间:2023-12-02 11:24:49 24 4
gpt4 key购买 nike

我正在序列化的类中有一个 writeObject 方法,并且我在其中调用 defaultWriteObject。这里有什么问题吗?字段密码是我尝试加密然后自行解密的 transient 字段。当我运行此代码时,我在 defaultWriteObject() 处收到 NotActiveException。任何帮助将不胜感激,谢谢:)

public static void main(String[] args) throws IOException, 
ClassNotFoundException {
Account bankAccount;
bankAccount = new Account("Person", 123456789, "Pa55word", 900);

try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("P:/Account.txt"));
bankAccount.writeObject(out);
out.close();
Account otherAccount = new Account();

ObjectInputStream in = new ObjectInputStream(new FileInputStream("P:/Account.txt"));
otherAccount.readObject(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException {
out.defaultWriteObject();
out.writeObject(encrypt(password));
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
password = decrypt((String)in.readObject());
}

这是堆栈跟踪:

java.io.NotActiveException: not in call to writeObject
at java.io.ObjectOutputStream.defaultWriteObject(Unknown Source)
at Account.writeObject(Account.java:75)
at Account.main(Account.java:59)

最佳答案

问题出在这里:bankAccount.writeObject(out);

您需要将对象写入ObjectOutputStream,然后Serialized将调用您的方法writeObject并保存它。

尝试:out.writeObject(bankAccount)

关于java - 我在 defaultWriteObject 处收到 NotActiveException,但我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49698315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com