I creating a program that will calculate something and store the information into a hashTable. What I want it to do is the next time it runs to restore the previous values stored in the hashTable and resume. So I ask how to save an object to a file?
In my code, I am using FileInputStream and FileOutputStream. Here is a snip-bit of my code, so far:
@SuppressWarnings("unchecked")
private void obtainSaveFile(){
String file = new String("./saveFile.txt");
try{
//Create the file if it doesn't exist
if( !( (new File(file)).exists() ) ){
//Create new empty file
(new File(file)).createNewFile();
//Creates a fresh new hashTable
hashTable = new QuadraticProbingHashTable<TicTacToeBoard>();
}
else{
//Obtain the hashTables saved config.txt file
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
//Obtain the previously saved hashTable
//-----------------------------------
//This line of code gives me an error
//-----------------------------------
hashTable = (QuadraticProbingHashTable<TicTacToeBoard>)ois.readObject();
ois.close();
}
//Create save file configs.txt
FileOutputStream fos = new FileOutputStream(file);
saveFile = new ObjectOutputStream(fos);
}catch(Exception e){
e.printStackTrace();
//Creates a fresh new hashTable
//This line of code gives me an error
hashTable = new QuadraticProbingHashTable<TicTacToeBoard>();
}
}
Initially, when there is no file, it runs fine. But after that, it keeps giving me errors. It doesn’t like when I take an object from the file and cast it as a QuadraticProbingHashTable and set that equal to hashTable.
Also, here is what is outputed as the error:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at proj4.TicTacToeSolver.obtainSaveFile(TicTacToeSolver.java:308)
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:120)
at proj4.Main.main(Main.java:41)
Caused by: java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:246)
... 1 more
Exception in thread "main" java.lang.NullPointerException
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:246)
at proj4.Main.main(Main.java:41)
Well, the problem seems to be, that you are trying to serialize something which does not implement Serializable.
You can see that here:
You have two choices:
For more details you could post the class.