NotSerializableException even after implementing Serializable

风流意气都作罢 提交于 2019-12-12 04:49:28

问题


Recieving the error:

java.io.NotSerializableException

even after implementing Serializable.

I'm simply trying to serialize a Survey object. Here is my code, does anyone have an idea why I am receiving this error?

public void loadData(Survey survey, Test test, Boolean isSurvey) throws FileNotFoundException {

    ...

    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
        Survey s = (Survey) ois.readObject();
        ois.close();
        System.out.println("list size = " + s.questions);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void saveData(Survey survey, Test test, Boolean isSurvey, String fileName) {

    ...

    if (isSurvey) {
        try {
            FileOutputStream fileOut = new FileOutputStream("surveys/" + fileName);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(survey);
            out.close();
            fileOut.close();
        } catch (IOException ex) {
        }
    } else {
        try {
            FileOutputStream fileOut = new FileOutputStream("tests/" + fileName);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(test);
            out.close();
            fileOut.close();
        } catch (IOException ex) {
        }
    }
}

EDIT: Added survey class. My Question class, Main class, and Input class both implement Serializable

package Survey;
import java.io.Serializable;
import java.util.ArrayList;

import IO.Input;
import Question.Question;

public class Survey implements Serializable{

private static final long serialVersionUID = 1L;
protected Main main = new Main();
protected Input input = new Input();

protected String name;
public ArrayList<Question> questions = new ArrayList<Question>();

public void setName() {
...
}

public void displayName() {
...
}

public void displayQuestions() {
...
}

public void mainMenu() {
...
}

public void addQuestionMenu() {
...
}

}

来源:https://stackoverflow.com/questions/26598397/notserializableexception-even-after-implementing-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!