8 Replies Latest reply: Jul 31, 2008 7:34 AM by 807589 RSS

    Custom serialization NullPointerException problem

    807589
      Dear all,

      I've got a question concearing the below code which depicts my problem in a more complicated project.
      I've got a simple inheritance hierarchy:
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.Serializable;
      
      public abstract class ForSerialization implements Serializable {
      
           protected transient String transientName;
      
           protected String forSerializationName;
      
           protected ForSerialization() {
      
           }
      
           public ForSerialization(String name) {
                init(name);
           }
      
           protected final void init(String name) {
                this.transientName = name;
           }
      
           public String toString() {
                return "My name is: " + transientName;
           }
      
           protected void hibernate() {
                forSerializationName = new String(transientName);
           }
      
           protected void reviwe() {
                if (forSerializationName == null) {
                     System.out.println("Ooops.");
                }
                transientName = new String(forSerializationName);
                // Let's clean this copy as it is no longer needed
                forSerializationName = null;
           }
      
      }
      and
      import java.io.IOException;
      import java.io.NotSerializableException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.Serializable;
      
      public class ForSerializationChild extends ForSerialization implements Serializable {
      
           ForSerializationChild(String name) {
                super(name);
                // TODO Auto-generated constructor stub
           }     
           private static final long serialVersionUID = 1L;
      
           public String toString() {
                return "Child: calls super toString(): " +
                super.toString();
           }
      }
      and two methods:
             private void writeObject(ObjectOutputStream out) throws IOException {
                hibernate();
                out.defaultWriteObject();
      
           }
      
           private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
                in.defaultReadObject();          
                reviwe();
      
           }
      and a tester class:
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      
      public class InhericySerializationTest {
      
           public static void main (String []args) {
                String filename = "tests.persistent";
                System.out.println("Serializing");
                FileOutputStream fos = null;
                ObjectOutputStream out = null;
                try {
                     File f = new File(filename);
                     System.out.println(f.getAbsolutePath());
                     fos = new FileOutputStream(filename);
      
                     out = new ObjectOutputStream(fos);
                     out.writeObject(new ForSerializationChild("Serialization test name"));
                     out.close();
                } catch (IOException ex) {
                     ex.printStackTrace();
                }          
      
                FileInputStream fin = null;
                ObjectInputStream oin = null;
      
                System.out.println("Deserializing");
                try {
                     fin = new FileInputStream(filename);
                     oin = new ObjectInputStream(fin);
                     ForSerializationChild fsc = (ForSerializationChild)oin.readObject();
                     System.out.println(fsc.toString());
                } catch (IOException e) {
                     e.printStackTrace();
                } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           }
      }
      When I put the two methods in parent class everything seems to be working fine.

      But when I put them in child class I get a NullPointerException and a "Oops" message while deserializign and it's not a file reading issue but rather a problem with line commented as // Let's clean this copy as it is no longer needed. Can anybody please explain to me why in parent it works and in child it does not.

      Regards,