0

I'm currently trying to save and load multiple instances of a class of type 'Player' via serialization. (This is for an assignment as an fyi).

The way the process currently works is I run the class via an ObjectOutputStream.writeObject(Player), this seems to work as I get the text file with what appears to be class data.

The problem I'm currently having is when I reverse and load the class, I'm unable to cast the object back to a player object, it is throwing an exception, however I'm not currently knowledgeable enough to figure out the exact exception.

Please find below the functions.

Save function:

public void savePlayers(ArrayList<Player> p)
{
    FileOutputStream fout = null;
    ObjectOutputStream oos = null;
    try
    {
        fout = new FileOutputStream("Player.txt");
        oos = new ObjectOutputStream(fout);
        for(Player player: p)
        {
          oos.writeObject(p);
        }
        fout.close();
        oos.close();
      }

    catch(Exception e)
    {
        System.out.println("Error is: " + e);
    }
    finally
    {
        closeStreams(fout, oos);
    }

}

Load Function:

public ArrayList<Player> loadPlayers() 
{   

    ArrayList<Player> loadedList = new ArrayList<Player>();

    FileInputStream fis;
    ObjectInputStream ois;

    try
    {
        fis = new FileInputStream("Player.txt");
        ois = new ObjectInputStream(fis);

       while(true)
       {
           System.out.println("Entered loop"); //testing
           Object obj = ois.readObject();
           System.out.println("Read object done"); //testing
           Player p = (Player)obj;
           System.out.println("Casted player"); //The line here doesn't run, prints out error from exception instead and exits loop
           loadedList.add(p);
           System.out.println("Loop iterated");
       }
    }
    catch(EOFException eof)
    {

    }
    catch(Exception e)
    {
           System.out.println("Exception here");
    }
}

Is there a better way to save the classes? Am I doing something wrong when casting?

4
  • why won't you print the actual error message and see what it says? Commented Oct 17, 2017 at 2:07
  • How is Player defined? Also do not silently swallow exceptions Commented Oct 17, 2017 at 2:10
  • Whoops! Teacher had typed that to see where the error was coming from and I forgot to change it, it comes with: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: australianopen.GameController. Going to have to scour through that class and figure out what's going on Commented Oct 17, 2017 at 2:13
  • Player is defined in its own class, it implements serializable, the arraylist containing players is contained in another class, and then that arraylist is passed through the function as above. Commented Oct 17, 2017 at 2:18

1 Answer 1

2

change to

oos.writeObject(player);

and the moment you are writing the whole ArrayList multiple times

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'm now not getting exceptions or errors, hopefully I can fix the rest of the spaghetti and finally get some players loaded!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.