【问题标题】:Java NotSerializableException:Java NotSerializableException:
【发布时间】:2014-07-23 04:22:32
【问题描述】:

我收到以下错误:

java.io.NotSerializableException: cscie55.project.AccountInfo

我的 AccountInfo 类没有实现 java.io.Serializable. 我必须实现它吗 Serializable?

如果是这样,有人可以帮助我怎么做吗?

以下是我的客户端类的一部分:

public class Client extends UnicastRemoteObject implements ATMListener {

    public Client() throws RemoteException {

    }

    private static AccountInfo getAccountInfo(int accountNumber, int pin) {
        //AccountInfo accountInfo = new AccountInfo(accountNumber, pin);
        return new AccountInfo(accountNumber, pin);
    }

    public static void main(String[] args) {
        ATM atm;

        try {
            ATMFactory factory = (ATMFactory)Naming.lookup("atmfactory");
            atm = factory.getATM();
            Client clientListener = new Client();
            atm.addListener(clientListener);
            Client.testATM(atm);
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (NotBoundException nbe) {
            nbe.printStackTrace();
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
        } catch (RemoteException re) {
            re.printStackTrace();
        }

    }

AccountInfo 类:

public final class AccountInfo {

    private final int accountNumber;
    private final int pin;

    public AccountInfo(int accountNumber, int pin) {
        this.accountNumber = accountNumber;
        this.pin = pin;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public int getPin() {
        return pin;
    }

}

在 BankImpl 类中创建帐户:

public class BankImpl extends UnicastRemoteObject implements Bank {

    public static LinkedHashMap<Integer, Account> accounts;

    public BankImpl() throws RemoteException {

        accounts = new LinkedHashMap<Integer, Account>();

        //constructor creates 3 accounts
        Account account1 = new Account(0000001, 1234, 0, true, true, true);
        Account account2 = new Account(0000002, 2345, 100, true, false, true);
        Account account3 = new Account(0000003, 3456, 500, false, true, true);

        //assigns all the accounts to a collection
        accounts.put(0000001, account1);
        accounts.put(0000002, account2);
        accounts.put(0000003, account3);
    }

【问题讨论】:

    标签: java serialization serializable


    【解决方案1】:

    让您的 AccountInfo 类实现 Serializable:

    public final class AccountInfo implements Serializable {
    

    您不需要添加任何其他方法,但是为了安全起见,您应该在 AccountInfo 类中定义一个 serialVersionUID:

    private static final long serialVersionUID = insert_random_long_here;
    

    您可以使用本站生成随机的serialVersionUID:http://random.hd.org/getBits.jsp?numBytes=0&type=svid

    【讨论】:

    • 如果我这样做,我将不再看到来自客户端的 main 方法的任何输出。它甚至没有运行 ATMFactory factory = (ATMFactory)Naming.lookup("atmfactory");
    【解决方案2】:

    我的 AccountInfo 类没有实现 java.io.Serializable。一定要序列化吗?

    否,但您的问题表明您正在对其进行序列化。我想你的意思是'我必须做到Serializable?。如果您打算对其进行序列化,答案是“是”。

    如果它正在被序列化并且您不希望它被序列化,请在正在被序列化的对象中引用它transient

    注意UnicastRemoteObject 是服务器,而不是客户端。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2018-08-23
      • 2015-12-11
      • 2012-05-09
      • 2014-09-09
      • 1970-01-01
      • 2018-10-04
      • 2015-06-01
      • 2013-05-14
      相关资源
      最近更新 更多