0

I am trying to serialize and then deserialize multiple objects. I can write to a file without any problems, but I get the following stack trace when selecting my deserialize option. Previously I could serialize and deserialize one object successfully.

Stack trace in dos is:

java.io.EOFException
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Sourc
e)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at mainmenutest.DeserializeDemo.Deserialize(DeserializeDemo.java:23)
        at mainmenutest.MainmenuTest.getInput(MainmenuTest.java:64)
        at mainmenutest.MainmenuTest.main(MainmenuTest.java:26)

My de-serialization code is as follows :

package mainmenutest;

/**
 *
 * @author Darren Estcourt
 */
import java.io.*;
public class DeserializeDemo
{
   public void Deserialize()
   {
      ClubInfo club = null;
      ClubInfo club2 = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("C:/tmp/club.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         club = (ClubInfo) in.readObject();
         club2 = (ClubInfo) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Club class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Saved game loaded...");
      System.out.println("Name: " + club.teamName);
      System.out.println("Stadium: " + club.stadium);
      System.out.println("Division: " + club.division);
     // System.out.println("SSN: " + club.SSN);
      System.out.println("Stadium Capacity: " + club.stadiumCapacity);
      System.out.println("Name : " + club2.teamName);

    }
}

I don't want someone to fix it for me, just a pointer in the right direction. I plan to deserialize around 20 objects eventually.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • 1
    Signals that an end of file or end of stream has been reached unexpectedly during input.This exception is mainly used by data input streams to signal end of stream (From oracle site). Publish the code on how you are serializing the object to cross check with de serialization. – Ravindra babu Sep 13 '15 at 21:45
  • Pointer in the right direction [here](http://stackoverflow.com/questions/10849449/how-to-prevent-inputstream-readobject-from-throwing-eofexception) – Manos Nikolaidis Sep 13 '15 at 21:47

1 Answers1

1

EOFException is thrown when there are no more objects to read, or when the stream unexpectedly reaches its end for some other reason (zero length, it got truncated, it wasn't flushed properly when closed).

In this case the exception was thrown by readObject(), so only the first applies. Looks like you either serialized no objects and are trying to read one, or you serialized one object and are trying to read two.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I am using 'if statements' in my seriailization code via a menu. So if option 1 is selected, object one is serialized, but not object two. However I have realised that one problem is I am trying to de-serialize both objects, even if only one object has been invoked. Not sure if this will fix everything, but it's an obvious problem that I overlooked. Thanks for your insight and wisdom all :) – Darren Estcourt Sep 14 '15 at 11:57
  • Serialization code, is as follows, as before, I think I need to de-serialize with if statements as well. – Darren Estcourt Sep 14 '15 at 22:00
  • Unless you're serializing and deserializing in the same process, you can't know how many object were serialized, so no amount of `if` statements is going to help you. Just read the stream until you get the `EOFException.` – user207421 Sep 14 '15 at 22:51