Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

serialization question

I am trying to serialize a LinkedList full of objects, but every time I deserialize the LinkedList, the instance variables in the objects are set to null.  I know that they have been initialized to some value before the serialization.

Here is my code:

import java.io.*;
import java.util.*;

public class Test {
   
    static LinkedList theList = new LinkedList();
   
    public static void main(String[] args) {
        try {
            java.io.FileInputStream inFile = new java.io.FileInputStream ("c:\\email\\back\\emails.dat");
            java.io.ObjectInputStream inStream = new java.io.ObjectInputStream(inFile);
            theList = (java.util.LinkedList) inStream.readObject();
            inStream.close();
        }
     catch (java.io.FileNotFoundException e) {
         theList = new java.util.LinkedList();
     }
     catch (java.io.IOException e) {
            System.out.println(e);
     }
     catch (java.lang.ClassNotFoundException e) {
         System.out.println(e);
     }
       
        if(theList.size() > 0) {
            Email theEmail;
            theEmail = (Email)theList.get(0);
            System.out.println(theEmail);
        }
        else {
            Email theEmail = new Email(0, "dude", "this", "sucks");
            theList.add(theEmail);
        }
        try {
            java.io.FileOutputStream outFile = new java.io.FileOutputStream("c:\\email\\back\\emails.dat");
            java.io.ObjectOutputStream outStream = new java.io.ObjectOutputStream(outFile);
            outStream.writeObject(theList);
            outStream.flush();
            outStream.close();
            System.out.println("WRITING");
     }
     catch (java.io.NotSerializableException e) {
            System.out.println("Problem: " + e);
     }
     catch (java.io.IOException e) {
            System.out.println(e);
     }
    }
}

//Here is the Email class code

public class Email implements java.io.Serializable {
   
    private static String name;
    private static String email;
    private static String subject;
    private static int mailNumber;
   
    public Email(int theMailNumber, String theSenderName,
                 String theSenderEmail, String theSubject) {
        name = theSenderName;
        email = theSenderEmail;
        subject = theSubject;
        mailNumber = theMailNumber;
    }
   
    public static String returnName() {
        return name;
    }
    public static String returnEmail() {
        return email;
    }
    public static String returnSubject() {
        return subject;
    }
   
    public String toString() {
        String returnString;
        returnString = "From: ";
        if(name == null)
            returnString = returnString + email;
        else
            returnString = returnString + name;
        returnString = returnString + "\tSubject: " + subject;
       
        return returnString;
    }
}


The first time the program is run, it creates an Email object, and adds it to the LinkedList.  It then serializes the LinkedList.  The second time the program is run, it should deserialize the LinkedList, and print out the Email object.  What gets printed for me is this:

From: null      Subject: null

What am I doing wrong?
0
lwinkenb
Asked:
lwinkenb
  • 3
  • 2
1 Solution
 
objectsCommented:
You declare all the member variables od Email as static:

  private static String name;
   private static String email;
   private static String subject;
   private static int mailNumber;

Remove static from all of these and it should work.

0
 
lwinkenbAuthor Commented:
yep, you're right.  Just out of curiousity, why doesn't it work with the static variables?
0
 
objectsCommented:
static variable are class variables and 'belong' to the Email Class instance, not each instance of an Email object.

They would thus get stored if you serialized the Email.class object.
0
 
lwinkenbAuthor Commented:
Thanks again
0
 
objectsCommented:
No worries :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now