Solved

serialization question

Posted on 2002-07-02
5
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

728 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