Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

serialization question

Posted on 2002-07-02
5
Medium Priority
?
198 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 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 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