Andre Nagel
Andre Nagel

Reputation: 73

Java all values null when deserializing an array list

I'm making a java program as a challenge to myself to learn, and I'm currently having trouble with serializing and de-serializing an arraylist. When I de-serialize all of the values are null.

This is the function that initially serializes the list:

private void saveModList(ArrayList<Moderator> m) {
    try {
        ObjectOutputStream fOut = new ObjectOutputStream(new FileOutputStream("data/modlist.ctm"));
        fOut.writeObject(m);
        fOut.close();
    } catch(IOException ex) {
       JOptionPane.showMessageDialog(null, "Could not save moderator list.",
           "Save error", JOptionPane.ERROR_MESSAGE);
       ex.printStackTrace();
    }
}

This is the function that deserializes the list:

public static ArrayList<Moderator> openModList() {
    try {
        ObjectInputStream fIn = new ObjectInputStream(new FileInputStream("data/modlist.ctm"));

        try {
            return (ArrayList<Moderator>) fIn.readObject();
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Could not open moderator list",
              "Read error", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }

        fIn.close();
    } catch(FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Could not load moderator data. File not found.",
            "Moderator file not found.", JOptionPane.ERROR_MESSAGE);
           ex.printStackTrace();
    } catch(EOFException ex) {

    } catch(IOException ex) {
        JOptionPane.showMessageDialog(null, "Could not load moderator data.",
        "Error", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }

    //If something screws up, return null, and the user will not be logged in
    return null;
}

And this calls the function to deserialize

ArrayList<Moderator> modLoginList = new ArrayList<Moderator>();

modLoginList = Main.openModList();

//Check all of the moderators.
//If one of them matches up to a moderator username and password, log them in
for(int i = 0; i < modLoginList.size(); i++) {
    if(modLoginList.get(i).name.equals(username) && modLoginList.get(i).password.equals(password)) {
        loggedIn = true;
         break;
    }
}

When I do this, I also get a NullPointerException at the if statement, checking if the moderator's credentials are valid. When I go and just try and print out the values outright, they are null. The moderator class does implement serializable and has a serial version ID. Any suggestions on why this is happening and how to fix it/better ways to do this are greatly appreciated.

Also it is not just returning null outright because there was nothing to read, or something went wrong.

Upvotes: 0

Views: 1086

Answers (1)

Phillip W. Carver
Phillip W. Carver

Reputation: 36

The NullPointerException appears to come from the openModList method returning null. One option is to surround your for loop with a null check:

if (modLoginList != null) {
    for(int i = 0; i < modLoginList.size(); i++) {
        if(modLoginList.get(i).name.equals(username) && modLoginList.get(i).password.equals(password)) {
            loggedIn = true;
            break;
        }
    }
}


It's possible that the object reference you're serializing is null. So check the part of your code that adds objects to the ArrayList before passing it for serialization.


That said, your code could use other changes, such as instead of this block:

try {
    return (ArrayList<Moderator>) fIn.readObject();
} catch (ClassNotFoundException ex) {
    ...
    ex.printStackTrace();
}

fIn.close();

add a finally clause:

try {
    return (ArrayList<Moderator>) fIn.readObject();
} catch (ClassNotFoundException ex) {
    ...
    ex.printStackTrace();
} finally {
    fIn.close();
}

The finally clause will be executed regardless of what happens with the try/catch clauses.

Upvotes: 1

Related Questions