简体   繁体   English

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

[英]I'm getting a NotActiveException at defaultWriteObject and I can't tell why

I have a writeObject method in the class I'm serializing, and I'm calling defaultWriteObject in it. 我要序列化的类中有一个writeObject方法,并且在其中调用defaultWriteObject。 What is wrong here? 怎么了 The field password is the transient field that I'm trying to encrypt then decrypt on my own. 字段密码是我尝试加密然后自己解密的临时字段。 When I run this code, I get a NotActiveException at defaultWriteObject(). 当我运行此代码时,在defaultWriteObject()处收到NotActiveException。 Any help would be appreciated, thanks :) 任何帮助,将不胜感激,谢谢:)

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());
}

Here is the stack trace: 这是堆栈跟踪:

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)

The problem is in here: bankAccount.writeObject(out); 问题出在这里: bankAccount.writeObject(out);

You need to write your Object into ObjectOutputStream than Serializable will call your method writeObject and will save it. 您需要将Object写入ObjectOutputStream ,然后Serializable将调用方法writeObject并将其保存。

Try to : out.writeObject(bankAccount) 尝试: out.writeObject(bankAccount)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到一个nullpointerexception,我找不到原因 - I'm getting a nullpointerexception and I can't find why 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 你能告诉我为什么我收到“字符串无法转换为 int”错误 - Can you tell me why I'm getting a "string cannot be converted to int" error 无法弄清楚为什么我得到了StringIndexOutOfBoundsException - Can't figure out why I'm getting a StringIndexOutOfBoundsException 无法理解为什么我得到一个空指针错误 - Can't understand why i'm getting a null pointer error 我怎样才能知道为什么在com.sun.jdmk.comm.HttpSendSocket的第113行出现IOException - How can I tell why I'm getting an IOException at line 113 in com.sun.jdmk.comm.HttpSendSocket 我不知道为什么 JLabel 没有显示 - I can't tell why JLabel is not showing 我在Ebay API上收到400错误的请求,我不知道为什么 - I'm getting a 400 Bad Request on the Ebay API and I can't figure out why 我得到一个ArrayIndexOutOfBounds异常,我不知道为什么。 有人可以解释一下吗? - I'm getting an ArrayIndexOutOfBounds Exception and I don't know why. Can someone shed some light? 我无法打印出 0。有人可以告诉我为什么吗? - I can´t print out 0 . Can someone tell me why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM