ParseTheData
ParseTheData

Reputation: 147

Using java.io.Serializable when implementing a tree?

I have ANOTHER serialization question, but this time it is in regards to Java's native serialization import when serializing to binary. I have to serialize a random tree that is generated in another java file. I know how serialization and deserialization works, but the example I followed when using binary serialization with java.io.Serializable did not work in the same fashion as when I did it with, say a simple object. Here is my code segment:

import java.io.*;
import java.io.FileInputStream;

public class BinaryS 
    {

    public static void main(String[] args) {
        Tree randomTree = RandomTreeBuilder.randomTree(10);

        FileOutputStream fOut=null;
        ObjectOutputStream oOut=null;

        try{
            fOut= new FileOutputStream("/Users/Pat/programs/binaryfile.txt");
            oOut = new ObjectOutputStream(fOut);
            oOut.writeObject(randomTree); //serializing randomTree
            System.out.println("An employee is serialized into /Users/Pat/binaryfile.txt");
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                oOut.flush();
                oOut.close();
                fOut.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
});

I believe the problem is when I use writeObject(randomTree). I get some terminal exceptions when this happens... they are below:

java.io.NotSerializableException: GeneralTree at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302) at BinaryS.main(BinaryS.java:24)

edit: I know it says GeneralTree, but at the start of the class it was in I put

print("public class RandomTreeBuilder implements java.io.Serializable");

then, GeneralTree is found below it

print(" protected static Tree tree;
protected static ArrayList names;
//e6.1

/**
 *Builds a random tree.  The build method does the work.
 */
//b6.2
public static Tree randomTree(int n) {
    // Create a random binary tree with n external nodes
    tree = new GeneralTree();
    names = NameGenerator.getNames();
    build(tree.getRoot(), n);  // auxiliary recursive method
    return tree;

");

Update: Hey guys, I figured out my own problem, turns out I am an idiot and didn't realize I had to download an additional .java file, an easy fix now! Thanks for your help!

Upvotes: 1

Views: 3072

Answers (2)

McDowell
McDowell

Reputation: 108979

edit: I know it says GeneralTree, but at the start of the class it was in I put

print("public class RandomTreeBuilder implements java.io.Serializable");

This does you no good - you are not trying to write an object of type RandomTreeBuilder to the object stream; the type you are trying to write is GeneralTree and it is that class that must implement Serializable.


FYI: in case you haven't come across it, this article covers many of the tricks and caveats of serialization: Discover the secrets of the Java Serialization API.

Upvotes: 0

Powerlord
Powerlord

Reputation: 88846

At a guess, GeneralTree doesn't implement the Serializable marker interface, as documented here.

Actually, it could also be the objects you're storing in the tree aren't Serializable. A Collection is only serializable if all the elements in it are as well.

Upvotes: 4

Related Questions