即使在实现Serializable之后仍然存在NotSerializableException

时间:2014-10-27 23:11:11

标签: java serialization deserialization serializable

收到错误:

java.io.NotSerializableException

即使在实施Serializable。

之后

我只想尝试序列化调查对象。这是我的代码,有没有人知道我收到此错误的原因?

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) {
        }
    }
}

编辑:增加了调查课程。我的Question类,Main类和Input类都实现了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() {
...
}

}

0 个答案:

没有答案