I have a program that runs a binary search tree and lists a menu of options. Two of the options are the only ones I can't seem to get to work which are saveToFile and buildFromFile. These methods include asking for a name to save as an output file (save) and asking for a name of the input file (open). Say I wanted to save a whole binary tree, nodes and all, and then be able to load that file and pick up right where I left off. Sounds easy, right? Apparently it's too hard for me hahaha. I get a null pointer exception at the saveTree() call, but I can see the file in WinSCP.
Error for saving a tree with multiple nodes:
java.io.NotSerializableException: treePkg.TreeNode
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at treePkg.Tree.saveTree(Tree.java:244)
at treePkg.Tree.saveToFile(Tree.java:217)
at TreeTest.main(TreeTest.java:98)
Error for loading an existing file:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException : treePkg.TreeNode
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
at treePkg.Tree.buildFromFile(Tree.java:270)
at TreeTest.main(TreeTest.java:102)
Caused by: java.io.NotSerializableException: treePkg.TreeNode
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at treePkg.Tree.saveTree(Tree.java:243)
at treePkg.Tree.saveToFile(Tree.java:217)
at TreeTest.main(TreeTest.java:98)
Here's my code thus far.
Any help is greatly appreciated :)
//SAVE
public boolean saveToFile(){
String name;
boolean save = true;
System.out.print("Name of the output file: ");
name = input.nextLine();
try{
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(name));
saveTree(output,root);
System.out.print("Saved.");
save = true;
output.close();
}
catch(FileNotFoundException fnfe){
System.out.println("File not found.");
return false;
}
catch(SecurityException se){
System.out.println("No write access to this file.");
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
return save;
}
private boolean saveTree(ObjectOutputStream output, TreeNode<T> tree){
//boolean save = saveTree(output,root);
try{
output.writeObject(tree.data);
//saveTree(output,tree.leftNode);
//saveTree(output,tree.rightNode);
output.writeObject(tree.leftNode);
output.writeObject(tree.rightNode);
}
catch(java.io.IOException exp){
exp.printStackTrace();
}
return true;
}
//END SAVE
//BUILD
public boolean buildFromFile(){
String name;
boolean end = false;
System.out.print("Name of the input file: ");
name = input.nextLine();
try{
//ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name));
FileInputStream fis = new FileInputStream(name);
ObjectInputStream ois = new ObjectInputStream(fis);
if(input==null)
System.out.println("\nInput file did not open.\n");
while(end==false)
{
T obj = (T)ois.readObject();
insertItem(obj);
System.out.print("Opened.");
}
input.close();
}
catch(FileNotFoundException fnfe){
System.out.println("File not found.");
return false;
}
catch(NoSuchElementException nsee){
System.out.println("Invalid input. Please try again");
return false;
}
catch(IllegalStateException ise){
System.out.println("Illegal state.");
return false;
}
catch(EOFException eofe){
return true;
}
catch(Exception e){
System.out.println("Error opening file.");
return false;
}
return end;
}
//END BUILD