Solved

serialization question

Posted on 2002-07-02
5
192 Views
Last Modified: 2010-03-31
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
Comment
Question by:lwinkenb
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7125897
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
 
LVL 5

Author Comment

by:lwinkenb
ID: 7125972
yep, you're right.  Just out of curiousity, why doesn't it work with the static variables?
0
 
LVL 92

Expert Comment

by:objects
ID: 7125987
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
 
LVL 5

Author Comment

by:lwinkenb
ID: 7126417
Thanks again
0
 
LVL 92

Expert Comment

by:objects
ID: 7126428
No worries :-)

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question