首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java NotSerializableException:

Java NotSerializableException:
EN

Stack Overflow用户
提问于 2014-06-02 23:29:58
回答 2查看 135关注 0票数 0

我得到了以下错误:

代码语言:javascript
复制
java.io.NotSerializableException: cscie55.project.AccountInfo

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

如果是的话,谁能帮我做这件事吗?

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

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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类中创建:

代码语言:javascript
复制
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);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-02 23:36:02

使您的AccountInfo类实现可序列化:

代码语言:javascript
复制
public final class AccountInfo implements Serializable {

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

代码语言:javascript
复制
private static final long serialVersionUID = insert_random_long_here;

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

票数 3
EN

Stack Overflow用户

发布于 2014-06-03 01:42:56

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

没有,但您的问题表明您正在序列化它。我想你的意思是‘我必须让它成为Serializable?吗?答案是’是‘,如果你想序列化它的话。

如果它正在被序列化,并且您不希望它序列化,那么在被序列化的对象中对它进行transient引用。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24004891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档