1
package edu.uga.cs1302.gui;

import javax.swing.SwingUtilities;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class StudentMain {
    public static void main(String[] args) {
        // this is inside a "lambda" Runnable

        FileOutputStream fos;
        try {
            Student a = new Student("C", "H", 1234, "12-11-1937", "UGA");
            fos = new FileOutputStream("StudentsList.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            a.storeObject(oos);
            System.out.println("success");
            FileInputStream fis = new FileInputStream("StudentsList.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student b = new Student("B", "H", 8888, "10-22-1936", "UNC");
            b.retrieveObject(ois);
            System.out.println(b.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Student Class

package edu.uga.cs1302.gui;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.ParseException;

public class Student extends Person implements Serializable{

protected String college;
private static final long serialVersionUID = -5047600810798119921L;
public Student(String firstname, String lastname,int ID, String DOB1, String college) throws ParseException{
    super(firstname, lastname,ID, DOB1);
    this.college=college;
}
public void storeObject(ObjectOutputStream out) throws IOException{
    out.writeObject(this);
    out.flush();
}
public void retrieveObject(ObjectInputStream in) throws IOException,     ClassNotFoundException, ParseException{    
            Student temp = (Student) in.readObject();
        System.out.println(temp.toString());
        this.setFirst(temp.getFirst());
        this.setLast(temp.getLast());
        this.setID(temp.getID());
        this.setDOB(temp.getDOB());
        this.setCollege(temp.getCollege());

}
public String getCollege(){
    return college;
}
public void setCollege(String college){
    this.college= college;
}
public String toString(){
    String student = super.getID() + " "+super.getFirst()+" "+super.getLast()+" "+super.getDOB()+" ["+college+"]";
    return student;
}
}

Person class

package edu.uga.cs1302.gui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Person {

public String first;
public String last;
public Date date1;
public int ID;
public String DOB;

public Person(String firstname, String lastname,int ID, String DOB1) throws ParseException{
    this.first=firstname;
    this.last=lastname;
    if(ID>999&&ID<10000){
        this.ID = ID;
    }
    SimpleDateFormat simple = new SimpleDateFormat("MM-dd-yyyy");
    Date date = simple.parse(DOB1); //must parse to get data
    this.date1 = date;
    this.DOB = DOB1;
    //this.DOB = DOB;
}
public String getFirst(){
    return first;
}
public String getLast(){
    return last;
}
public String getDOB(){
    return DOB;
}
public int getID(){
    return ID;
}
public void setFirst(String first){
    this.first = first;
}
public void setLast(String last){
    this.last = last;
}
public void setDOB(String DOB1) throws ParseException{
    SimpleDateFormat simple = new SimpleDateFormat("MM-dd-yyyy");
    Date date = simple.parse(DOB1); //must parse to get data
    this.date1 = date;
}
public void setID(int ID){
    this.ID = ID;
}
public String toString(){
    SimpleDateFormat sim = new SimpleDateFormat("MM-dd-yyyy");
    String date = sim.format(this.date1);//formats to simple format
    String person = ""+ ID + " "+first+" "+last+" "+date;
    return person;
}


}

errors

success
java.io.InvalidClassException: edu.uga.cs1302.gui.Student; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:150)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:790)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1775)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at edu.uga.cs1302.gui.Student.retrieveObject(Student.java:25)
at edu.uga.cs1302.gui.StudentMain.lambda$0(StudentMain.java:30)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

for some unknown reason, my FileInputStream is not working. The file is getting created, so I know the FileOutputStream is working. But, when reading the file it can't decipher the meaning.

7
  • 2
    Try adding an empty constructor to Student class. Commented Nov 29, 2016 at 21:04
  • You could at an empty constructor, but if applicable you might want to use the constructor of Person, considering that's the entire point of inheritance is to extend what's already there. Commented Nov 29, 2016 at 21:08
  • You added the exception trace, which is good. Now you have to look at it. And when you do that, you'll see that the problem is not FileInputStream but ObjectInputStream. And when you look at the docs for InvalidClassException, you will know what to look for in your code. Or you could just follow Erik Kralj's advice. Commented Nov 29, 2016 at 21:14
  • when would I use the empty student constructor @ErikKralj Commented Nov 29, 2016 at 21:25
  • @DejaVuSansMono @kdgregory I can't use the Person constructor because it doesn't implement Serializable so I need to use Student. However, I believe the FileInputStream is storing my Student a as a String Commented Nov 29, 2016 at 21:29

2 Answers 2

1

If you have a non serializable class with no parameterless constructor, you can very likely get InvalidClassException during serialization and then de-serialization of any class that extends this calss. One of the solutions to this problem is to use Serialization Proxy pattern.

To implement it, you will have to create a static nested SerializationProxy class in the class which implements Serializable. SerializationProxy will represent the logical state of the enclosing class and will be written to the byte stream during serialization instead of the original instance and during de-serialization will create an instance of the original class which takes its place in the object graph.

https://dzone.com/articles/serialization-proxy-pattern

http://blog.codefx.org/design/patterns/serialization-proxy-pattern/

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

Comments

0

you need to have all your objects Serializable. Just implement the Serializable for Person as well so that it can be written.Infact, you should mark your base class serializable then child classes will automatically be serializable due to inheritance.

success 1234 C H 12-11-1937 [UGA] 1234 C H 10-22-1936 [UGA]

7 Comments

Why would I need 5 String arguments? As of right now, I have 4 String parameters and 1 integer parameter. @hhafeez
Student a = new Student("C", "H", 1234, "12-11-1937", "UGA"); here you passing 5 string arguments. But your instance expects 4. You passing one extra argument.
I see I missed seeing all arguments, your no of arguments in constructor are correct
My Student constructor expects (String firstname, String lastname,int ID, String DOB1, String college) so that shouldn't be the issue. or I would have had an error on line 12 but my error is on b.retrieveObject(ois) @hhafeez
yes I see it now. Can you tell me what kind of file is your StudentsList.dat.
|

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.