title: Java - Serialization - EOFException Issue
body: <p>Sorry for the second post today regarding serialization, fixing one issue caused another.</p>
  <hr />
  <p>As states here - <a href="https://stackoverflow.com/questions/24824161/java-serialization-notserializableexception-issue">Java - Serialization - NotSerializableException Issue</a> - I have a project with following classes</p>
  <blockquote>
  <p>Student.java</p>
  <p>StudentsCollection.java</p>
  </blockquote>
  <p>Students creates my Student object(Self explanatory) and my StudentsCollection() instantiates a list of type Student which stores my Student objects, when trying to save/load the objects I use this code and get the following exception thrown:</p>
  <pre><code>       /**
           * Open student collection
           */
         public void openCollection(){
            try {
               FileInputStream e = new FileInputStream(&quot;students.ser&quot;);
               ObjectInputStream inputSteam = new ObjectInputStream(e);
                while(inputSteam.readObject() != null){
                    this.list.add((Students)inputSteam.readObject());
                }
            } catch (FileNotFoundException var3) {
                var3.printStackTrace();
               JOptionPane.showMessageDialog(null, &quot;File not found&quot;);
            } catch (IOException var4) {
                var4.printStackTrace();
               JOptionPane.showMessageDialog(null, &quot;IO Exception&quot;);
            } catch (ClassNotFoundException var5) {
                var5.printStackTrace();
               JOptionPane.showMessageDialog(null, &quot;Required class not found&quot;);
            }
      
         }
  </code></pre>
  <p>And is throwing:</p>
  <pre><code>java.io.EOFException
      at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:558)
      at jdatabase.main.MainController.main(MainController.java:14)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>
  <p>I only have one Student object added to my list, after saving the list and re-opening it, I ask the console to print out that list, the Student actually prints out. However when I created multiple student objects and incremented their ID by 1 each time and added them, the console would print them ALL in order then re-print(for some odd reason) and end up skipping a few.</p>
  <p>If you need any more code just ask. The saveCollection() is working fine now</p>
  <p><strong>Revised code:</strong>
  /**</p>
  <pre><code>     * Open student collection
       */
     public void openCollection(){
        try {
           FileInputStream e = new FileInputStream(&quot;students.ser&quot;);
           ObjectInputStream inputSteam = new ObjectInputStream(e);
            while((obj = inputSteam.readObject()) != null){
                this.list.add((Students)obj);
            }
        } catch (FileNotFoundException var3) {
            var3.printStackTrace();
           JOptionPane.showMessageDialog(null, &quot;File not found&quot;);
        } catch (IOException var4) {
            var4.printStackTrace();
           JOptionPane.showMessageDialog(null, &quot;IO Exception&quot;);
        } catch (ClassNotFoundException var5) {
            var5.printStackTrace();
           JOptionPane.showMessageDialog(null, &quot;Required class not found&quot;);
        }
  
     }
  </code></pre>
  <p><em>Throws:</em></p>
  <pre><code>java.io.EOFException
      at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
      at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:559)
      at jdatabase.main.MainController.main(MainController.java:13)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  </code></pre>
  <p>Only have 1 student object and the following is printed in the console:</p>
  <blockquote>
  <p>Student name: Student
  Student surname: Default
  Student ID: 0 Student
  DoB: 1/1/90</p>
  </blockquote>
  <p>And printed once again:</p>
  <blockquote>
  <p>Student name: Student
  Student surname: Default
  Student ID: 0
  Student DoB: 1/1/90</p>
  </blockquote>