0

Possible Duplicate:
How to Serialize an ArrayLIst in java without getting errors?

I have two arraylists in two different classes. One is to store transaction objects temporarily & other for permanent. Im using a addAll method to copy objects from temporary arraylist to permanent. And then save objects from permanent arraylist to a file. When my program restarts, object in permanent arraylists are restored from the file. But Iam getting an exception. Whats wrong with my code ?

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList;

/** * * @author Haleemdeen */ public class FileImportExport {

private ArrayList<Stock> permntTransactions=new ArrayList<Stock>();


void cancatToPerrmntTransactions(ArrayList<Stock> arraylist1){
    permntTransactions.addAll(arraylist1);
}

ArrayList<Stock> displayPermntTransactions(){
    return permntTransactions;
}

    void exportToFile(){

    try{  // Catch errors in I/O if necessary.
    // Open a file to write to, named SavedObj.sav.
    FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");

    // Create an ObjectOutputStream to put objects into save file.
    ObjectOutputStream save = new ObjectOutputStream(saveFile);

    // Now we do the save.
    save.writeObject(permntTransactions);

    // Close the file.
    save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
    exc.printStackTrace(); // If there was an error, print the info.
    }
}

        void importFromFile(){

    try{
    // Open file to read from, named SavedObj.sav.
    FileInputStream saveFile = new FileInputStream("SaveObj.sav");

    // Create an ObjectInputStream to get objects from save file.
    ObjectInputStream save = new ObjectInputStream(saveFile);

    // Now we do the restore.
    // readObject() returns a generic Object, we cast those back
    // into their original class type.

    permntTransactions = (ArrayList<Stock>) save.readObject();

    // Close the file.
    save.close(); // This also closes saveFile.
    }
    catch(Exception exc){
    exc.printStackTrace(); // If there was an error, print the info.
    }

    // Print the values, to see that they've been recovered.
    System.out.println("\t\t" + permntTransactions);
    System.out.println();
} }

In main method :

FileImportExport file1=new FileImportExport();
file1.cancatToPerrmntTransactions(transactions);
file1.exportToFile();

This is what I get as exception:

java.io.NotSerializableException: m_media_cdstore.Stock at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) at java.util.ArrayList.writeObject(ArrayList.java:570) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:945)

1
  • Just make the type implement Serializable interface that you want to serialize.. to the highest level of depth of all the included types.. Commented Jan 6, 2013 at 8:24

3 Answers 3

5

The class you want to serialize (Stock, in this case) has to implement the Serializable interface.

Sign up to request clarification or add additional context in comments.

1 Comment

@haleem - assuming you're a beginner to serialization, it's probably a good idea to read a bit about serialization before they jump in (to avoid shooting yourself in the foot later). e.g. xyzws.com/Javafaq/…
3

The Stock class must implement Serializable, and all its fields (to full depth) must either implement Serializable or be primitive.

Comments

1

When trying to Serialize Object, make sure The particular Class (Stock.java) must implement the java.io.Serializable and if Stock class has any other classes those must implement java.io.Serializable

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.