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 - Retired Organizing Data Serialization

What to do if my Treats class is not serializable?

The error message:

Problem saving Treats
java.io.NotSerializableException: com.teamtreehouse.Treat
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStr eam.java:1184)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStrea m.java:1378)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStr eam.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStre am.java:348)
at com.teamtreehouse.Treats.save(Treats.java:10)
at Example.main(Example.java:25)

My code:

package com.teamtreehouse;

import java.io.*;

public class Treats { public static void save(Treat[] treats) { try ( FileOutputStream fos = new FileOutputStream("treats.ser");//open a new file, param is file name ObjectOutputStream oos = new ObjectOutputStream(fos);//to serialize array, param is string ) { oos.writeObject(treats);//oos provides a way to take representation in memory and write out its binary representation using a method called write object } catch(IOException ioe) { System.out.println("Problem saving Treats"); ioe.printStackTrace(); } }

}

There are two classes Treet and Treets in this example.

Treet is serializable by importing the interface:

import java.io.Serializable;

and

in the class declaration you add after Comparable, [Serializable].

Hope that helps, Amathia

4 Answers

import java.io.Serializable

in your class declaration. public class Treats implements Serializable {

That tells the compiler that your class is serializable.

Your class must implement serialize interface. on the class you want to save. In this instance treet or else treet can't be deflated so to speak.

How then do I make the Treat class serializable, if I understand correctly?

Thanks Ryan.