I can't figure out why is my class not serializable. As you can see below in my code my class implements Serializable interface.
public class Firm {
PrintWriter out = new PrintWriter(System.out, true);
Scanner sc = new Scanner(System.in);
Set<Worker> workers = new HashSet<>();
boolean saveToFile(){
String fileName = "text.txt";
ObjectOutputStream file;
boolean save = false;
try{
file = new ObjectOutputStream(new FileOutputStream(fileName));
file.writeObject(workers);
file.close();
save = true;
}catch(IOException e){
e.printStackTrace();
}
return save;
}
}
import java.io.Serializable;
public abstract class Worker implements Serializable {//Code
Any ideas how to fix it?
The problem is that the PrintWriter
and the Scanner
are not serializable; you can't serialize I/O connections like that.
Move those to wherever you're using them, if you need them at all, as local variables. Don't store them in the class.