gpt4 book ai didi

Java NotSerializedException :

转载 作者:行者123 更新时间:2023-12-01 18:31:24 28 4
gpt4 key购买 nike

我收到以下错误:

java.io.NotSerializableException: cscie55.project.AccountInfo

我的 AccountInfo 类未实现 java.io.Serialized。 我是否必须使其 Serialized?

如果可以,有人可以帮我解决吗?

以下是我的 Client 类的一部分:

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

最佳答案

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

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

关于Java NotSerializedException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004891/

28 4 0
文章推荐: java - 动态添加行后jtable中的垃圾值
文章推荐: Java 从 jar 文件加载图像
文章推荐: r -\usepackage{Sweavel} 产生错误 : It seems you are using the Sweave-specific syntax
文章推荐: java - 将 List 分解为一组已知派生类型的列表