Solved

serialization question

Posted on 2002-07-02
5
189 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
Comment Utility
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
Comment Utility
yep, you're right.  Just out of curiousity, why doesn't it work with the static variables?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
Thanks again
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
No worries :-)

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now