-1

I am working on a little quiz tool and face problems when I want to persist my objects (questions). This is my save method within the class Question, which imports "java.io.*":

public static boolean saveQuestion(String file, Question q){

    try{ 
        FileOutputStream saveFile=new FileOutputStream(file);
        ObjectOutputStream save = new ObjectOutputStream(saveFile);
        save.writeObject(q);
        save.close();
        return true;
    }
    catch(Exception exc){
    exc.printStackTrace(); 
    return false;
    }       
}

This is how I call the method from another class:

 Question q = new Question();
 Question.saveQuestion("question.sav",q);

When I try to run it, it throws a "java.io.NotSerializableException" at the save.writeObject(q);

When I change my code in order to just store an attribute of the object it works fine. What can be the problem?

Alex
  • 21,273
  • 10
  • 61
  • 73

1 Answers1

1

To serialize objects, your classes needs implements Serializable.

user207421
  • 305,947
  • 44
  • 307
  • 483
ByeBye
  • 6,650
  • 5
  • 30
  • 63