Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Organizing Data Serialization

Romel Liwag
Romel Liwag
4,183 Points

Im having an error here and I dont know why. My code is exactly the same as Craig Dennis.

package com.teamtreehouse;

import java.io.*;

public class Treets {
  public static void save(Treet[] treets) {
    try (
      FileOutputStream fos = new FileOutputStream("treets.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
    ) {
      oos.writeObject(treets);
    } catch(IOException ioe) {
      System.out.println("Problem saving Treets");
      ioe.printStackTrace();
    }
  } 
}

And this is the error i get

java.io.NotSerializableException: com.teamtreehouse.Treet                              
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)       
        at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1378)         
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)       
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)         
        at com.teamtreehouse.Treets.save(Treets.java:11)                               
        at Example.main(Example.java:21) 

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hmmm that error is saying your Treet class doesn't implement Serializable. Does it?