gpt4 book ai didi

java - 包含 ArrayList 的反序列化对象具有所有空值

转载 作者:行者123 更新时间:2023-12-01 13:09:34 26 4
gpt4 key购买 nike

希望有人能就这个问题提供一些见解。我创建了一个对象的实例,其中包含信息的 ArrayList(用户名、密码、密码提示)。我正在尝试序列化该对象。看起来序列化正常,但是当我重新启动项目进行反序列化时,它在 ArrayList 中返回空值。为什么它返回 ArrayList 对象的空值?

驱动程序类别:

import java.io.Serializable;


public class TestingDriver implements Serializable{

private static final long serialVersionUID = 12345L;
private static TestingAccount users = new TestingAccount();

public static void main(String[] args) {
int foreverLoop = 0;
users = DeSerialize.main();
while (foreverLoop < 1) {
int selection = users.displayMainMenu();

if (selection == 1) {
users.listUsers();
}
else if (selection == 2) {
users.addUser();
}
else if (selection == 3) {
users.deleteUser();
}
else if (selection == 4) {
users.getPasswordHint();
}
else if (selection == 5) {
Serialize.main(users);
System.exit(0);
}
else {
System.out.println("That option does not exist. Please try again.");
}
}
}
}

TestingUser 类(此类的对象将填充 ArrayList):

import java.io.Serializable;

public class TestingUser extends UserAccount implements Serializable, Comparable <TestingUser> {

private static final long serialVersionUID = 12345L;

public TestingUser(String username, String password, String passwordHint) {
super(username, password, passwordHint);
}

public TestingUser() {

}

@Override
public void getPasswordHelp() {
System.out.println("");
System.out.println("Password hint: " + passwordHint);
System.out.println("");
}

@Override
public int compareTo(TestingUser otherAccount) {
if (this.username.compareToIgnoreCase(otherAccount.username) < 0) {
return -1;
}
else if (this.username.compareToIgnoreCase(otherAccount.username) > 0) {
return 1;
}

else {
return 0;
}
}
}

TestingAccount 类(调用此类创建一个包含 ArrayList 的对象):

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;


public class TestingAccount implements Serializable {

private static final long serialVersionUID = 12345L;
public ArrayList<TestingUser> userList;
private String username;
private String password;
private String passwordHint;

public TestingAccount() {
userList = new ArrayList<TestingUser>();
}

public void listUsers() {

for (int i=0; i<this.userList.size(); i++) {
System.out.println(this.userList.get(i));
}
}

@SuppressWarnings("resource")
public void addUser() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a username: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
System.out.println("This user already exists.");
}
else {
System.out.println("Please enter a password: ");
password = input.next();
System.out.println("Please enter a password hint: ");
passwordHint = input.next();
tempAccount.password = password;
tempAccount.passwordHint = passwordHint;
this.userList.add(tempAccount);
System.out.println("Account " + tempAccount.username + " has been added.");
}
}

@SuppressWarnings("resource")
public void deleteUser() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the username to be deleted: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
int actIndex = this.userList.indexOf(tempAccount);
System.out.println("Please enter the password: ");
password = input.next();
tempAccount.password = password;
boolean passwordGood = this.userList.get(actIndex).CheckPassword(tempAccount);
int accountIndex = this.userList.indexOf(tempAccount);
tempAccount = this.userList.get(accountIndex);
if (passwordGood == true) {
this.userList.remove(actIndex);
System.out.println("The account has been deleted.");
}
else {
System.out.println("The password is not correct.");
}
}
else {
System.out.println("The account does not exist.");
}
}

@SuppressWarnings("resource")
public void getPasswordHint() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a username: ");
username = input.next();
TestingUser tempAccount = new TestingUser(username, null, null);
if (this.userList.contains(tempAccount) == true) {
int actIndex = this.userList.indexOf(tempAccount);
tempAccount = this.userList.get(actIndex);
System.out.println("The password hint isL: " + tempAccount.passwordHint);
}
else {
System.out.println("The account does not exist.");
}
}

@SuppressWarnings({ "resource" })
public int displayMainMenu() {
int selection = 0;
Scanner input = new Scanner(System.in);

System.out.println("");
System.out.println("System Menu:");
System.out.println("1. List Users");
System.out.println("2. Add User");
System.out.println("3. Delete User");
System.out.println("4. Get Password Hint");
System.out.println("5. Quit");
System.out.println("");
System.out.println("What would you like to do?");
selection = input.nextInt();
return selection;
}
}

序列化类:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;


public class Serialize {


public static void main(TestingAccount users) {

try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("serialize"));
oos.writeObject(users);
oos.close();
} catch (FileNotFoundException e1) {
System.err.println("File not found.");
} catch (IOException e2) {
System.err.println("Unable to serialize.");
e2.printStackTrace();
}
}
}

反序列化类:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;


public class DeSerialize {

public static TestingAccount main() {

TestingAccount deSerialize = null;

try {
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("serialize"));

deSerialize = (TestingAccount) ois.readObject();

ois.close();
} catch (FileNotFoundException e1) {
System.err.println("Unable to open file.");
} catch (IOException e2) {
System.err.println("Could not de-serialize.");
e2.printStackTrace();
} catch (ClassNotFoundException e3) {
System.err.println("Could not cast to class TestingAccount.");
}
return deSerialize;
}
}

最佳答案

看起来UserAccount不是可序列化的。因此,当您序列化派生的TestingUser类时,UserAccount 数据被序列化。请参阅Object Serialization Specification #2.1.13(a)TestingUser 没有任何自己的实例状态可以序列化。

解决方案是使UserAccount实现Serialized。

不知道为什么这会让人感到惊讶。

关于java - 包含 ArrayList 的反序列化对象具有所有空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22992032/

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