1

I am trying to serialize a TreeMap to a text file but I cannot get it to work. I have tried printing/debugging and reading Javadocs.

My Record and Table class both implement serializable and I've also built a custom comparator which also implements serializable for my new TreeMap<>().

I believe I have narrowed down the issues to:

  • I am trying to serialize Map.Entry, which might not be serializable and hence the NotSerializableExceptions.
  • TreeMap is/ is not serializable (?).
  • (Unknown Source) - unsure what Unknown Source is doing to my writeObject java.io.ObjectInputStream.readObject0(Unknown Source) and java.io.ObjectInputStream.writeObject0(Unknown Source)

My primary goal is to eliminate the NotSerializableExceptions by correctly serializing my Map.Entry or my TreeMap when i writeObject().

A secondary question would be what the 'readObject0(Unknown Source) ' is. In other cases resembling my issue, there was an ObjectOutputStream:1356 where my unknown source is.

I'm currently using Java 8.

class Record implements Serializable{

private String[] row;

class Table implements Serializable {

private TreeMap<String, Record> tm = new TreeMap<>(
        new ReverseNaturalOrderComparator());

private String[] columns;
private int totalRecords;
[...]
[...]
[...]
void insertRecord( String key, Record r ) { 
    if( r.rowArrayLength() == numRowFields ) {
        tm.put(key, r); 
        totalRecords++;
    }
    else { 
        System.out.println("Number of fields do not match. " + 
        "Please enter a record with " + numRowFields + " fields");
    }
}

class Files {

File file = new File("KeyValues.txt");

void writeFile(Table t) throws IOException {

    TreeMap<String, Record> ftm = t.getTreeMap();
    FileOutputStream os = new FileOutputStream(file);       
    ObjectOutputStream oos = new ObjectOutputStream(os);

    Set set = ftm.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()) {

        Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();

        if( !file.exists() ) { System.out.println("No such file exists"); }
        else { oos.writeObject(keyValue); } // <---causing error
    }
    oos.close();
    os.close();

}

The following code block within class Files is causing an error: Iterator i = set.iterator(); while(i.hasNext()) {

    Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();

    if( !file.exists() ) { System.out.println("No such file exists"); }
    else { oos.writeObject(keyValue); } // <---causing error
}
oos.close();
os.close();

} main():

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    Table t = new Table();

    Record test0 = new Record("We are the champions, my friend.");
    Record test1 = new Record("hope this works, but might not");
    Record test2 = new Record("sweet, yellow bananas");
    Record test3 = new Record("and bad mistakes Ihave made afew");
    Record test4 = new Record("Now using and printing a TreeMap<>");
    Record test5 = new Record("Exploring Antarctica alongside the polar bears");    

    t.insertRecord("3",test3);
    t.insertRecord("0",test0);
    t.insertRecord("2",test2);
    t.insertRecord("4",test4);
    t.insertRecord("1",test1);
    t.insertRecord("5",test5);

    try { writeFile(t); } //this is giving me the error.

Error Log

java.io.NotSerializableException: java.util.TreeMap$Entry
Final filepath : ...KeyValues.txt
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.writeFile(Files.java:52)
    at Files.test(Files.java:87)
    at Files.run(Files.java:64)
    at Files.main(Files.java:62)
java.io.WriteAbortedException: writing aborted; 
java.io.NotSerializableException: java.util.TreeMap$Entry
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Table.readFile(Table.java:92)
    at Files.test(Files.java:100)
    at Files.run(Files.java:64)
    at Files.main(Files.java:62)
Caused by: java.io.NotSerializableException: java.util.TreeMap$Entry
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.writeFile(Files.java:33)
    at Files.test(Files.java:66)
    at Files.run(Files.java:48)
    at Files.main(Files.java:46)
Javabytes
  • 11
  • 3
  • Can you show which line is line 33 of `Files.java`? – T Tse Mar 14 '18 at 23:45
  • https://stackoverflow.com/questions/13895867/java-io-notserializableexception Does this relate to what you're trying to solve? Do you have any `writeObject(entry)`? – T Tse Mar 14 '18 at 23:47
  • @ShioT line 33: oos.writeObject(keyValue); – Javabytes Mar 15 '18 at 00:01
  • I'm fairly confident that my issue is in Files.java:30-33 while(i.hasNext()) { Map.Entry keyValue = (Map.Entry)i.next(); if ( !file.exists() ) { System.out.println("No such file exists"); } else { oos.writeObject(keyValue); } – Javabytes Mar 15 '18 at 00:03
  • To determine whether TreeMap is serializable, go to [the documentation](https://docs.oracle.com/javase/9/docs/api/java/util/TreeMap.html) and read the **All Implemented Interfaces** section, a few lines below the top of the page. – VGR Mar 15 '18 at 11:33

1 Answers1

0

If a class which you would like to be serialized contains references of objects, class describing those objects should also implement Serializable (Record class in this case).

Edit: You can also try changing the File file = new File("KeyValues.txt"); to String file = "KeyValues.txt";

  • Hi, as written in my post, I did implement that across all classes and I also tried implementing my own new Comparator with Serializable. – Javabytes Mar 15 '18 at 00:08