包含ArrayList的DeSerialized Object具有所有空值

时间:2014-04-10 15:11:53

标签: java serialization arraylist deserialization

希望有人可以就此问题提供一些见解。我创建了一个对象的实例,其中包含一个信息的ArrayList(用户名,密码,密码提示)。我正在尝试序列化该对象。它看起来像是正确序列化,但是当我重新启动项目以进行反序列化时,它会在ArrayList中返回null值。为什么它返回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 Class(此类的对象将填充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;
    }
}

1 个答案:

答案 0 :(得分:1)

看起来UserAccount不是Serializable.因此,当序列化派生的TestingUser类时,UserAccount数据都不会被序列化。见Object Serialization Specification #2.1.13(a)TestingUser没有自己的任何实例状态来序列化。

解决方案是UserAccount实施Serializable.

不确定为什么会出乎意料。