0

I'm currently working on a student database using Java only and have two particular lists I would like to save. Students and Profiles(To login with). I'm currently testing out serialization on Students only to get it to work but have been getting a weird issue.


My Student object classes and code are as follows:

Student.java

StudentsCollection.java

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:

    /**
     * Save student collection
     */
   public void saveCollection(){
      try {
         FileOutputStream e = new FileOutputStream("students.ser");
         ObjectOutputStream outputStream = new ObjectOutputStream(e);
         for(Students i : this.list){
             outputStream.writeObject(i);
         }
         outputStream.flush();
         outputStream.close();
      } catch (IOException var3) {
          var3.printStackTrace();
         JOptionPane.showMessageDialog((Component)null, "Error. Cannot save database.");
      }

   }

    /**
     * Open student collection
     */
   public void openCollection(){
      try {
         FileInputStream e = new FileInputStream("students.ser");
         ObjectInputStream inputSteam = new ObjectInputStream(e);
          while(inputSteam.readObject() != null){
              this.list.add((Students)inputSteam.readObject());
          }
      } catch (FileNotFoundException var3) {
          var3.printStackTrace();
         JOptionPane.showMessageDialog(null, "File not found");
      } catch (IOException var4) {
          var4.printStackTrace();
         JOptionPane.showMessageDialog(null, "IO Exception");
      } catch (ClassNotFoundException var5) {
          var5.printStackTrace();
         JOptionPane.showMessageDialog(null, "Required class not found");
      }

   }

And the following exception is printed:

java.io.NotSerializableException: java.util.Scanner
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at jdatabase.objects.students.StudentsCollection.saveCollection(StudentsCollection.java:539)
at jdatabase.main.MainController$1.run(MainController.java:22)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I would also like to point out that students.ser is actually created in my project explorer but exceptions are still being thrown, even when trying to use openCollection() errors are thrown, however I'd like to tackle saveCollection() properly first

Community
  • 1
  • 1
Juxhin
  • 5,068
  • 8
  • 29
  • 55
  • 1
    I can't see it in your code snippets but the error reads as though it's trying to serialize a Scanner. By any chance does your Student class have a Scanner as a field? – Andrew Stubbs Jul 18 '14 at 11:42
  • Sigh you're right, once I lost my project I had asked a friend to decompile my JAR and some code jumbled up, the default constructor was requesting input for student name for some reason.. Will try removing all the Scanners and see if it works – Juxhin Jul 18 '14 at 11:43
  • Does any of your class have `Scanner` field? – Pshemo Jul 18 '14 at 11:44
  • Yes, I've found the issue. Thanks! – Juxhin Jul 18 '14 at 11:45

4 Answers4

5

You did not post your Student.java, so I am guessing here. Is there a field of type java.util.Scanner in the Student class? If there is, delete it, or mark it as transient:

private transient Scanner someField;

The transient modifier will prevent the field from being serialized.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
  • 1
    You can add some info that `transient` prevents serialization of field with this modifier. – Pshemo Jul 18 '14 at 11:46
  • Yes when my project was decompiled(lost source a while ago) it caused some weird code, all my constructors had some a Scanner requesting input for some reason – Juxhin Jul 18 '14 at 11:46
1

There is an Object Students in your this.list that references the Scanner. Remove the reference and you will be fine.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
1

You can serialize an object only if all of its class members are also implementing Serializable. As RobAu mentioned, Scanner is not serializeable.

You can find a good tutorial here: http://www.vogella.com/tutorials/JavaSerialization/article.html

All fields marked with transient will not be serialized. See here for more details: How does marking a field as transient make it possible to serialise an object

Community
  • 1
  • 1
Mirco
  • 2,940
  • 5
  • 34
  • 57
  • Yea I was aware of that, wasn't aware the Scanner objects were even in there that's why when reading that stack trace I was a bit confused! – Juxhin Jul 18 '14 at 11:47
0

Since scanner class does not implement Serializable interface, there must be some field in your class asking for input through scanner, hence the error.