Results 1 to 16 of 16
  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default Object serialization

    Hi,

    I have one question about object serialization: an object can write itself into a file or, in other words, both methods writeObject and readObject can be called from the same object we want to serialize?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Try it and see what happens.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    AFAIK, you don't call those methods on the objects themselves, but on the ObjectStreams that read and write the objects.

  4. #4
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    AFAIK, you don't call those methods on the objects themselves, but on the ObjectStreams that read and write the objects.
    I know, but my question was about the word "this" in this code, because in all the examples I've seen on the Internet they serialize an object different from the one which containes the code to serialize:

    Java Code:
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
    out.writeObject(this);
    out.close();

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    So did it work? Write a complete program that writes it out and reads it back in. If it works great, if not then you have a real question to post along with demo code.

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I follow this steps:
    - Make a new object of class Serialization, with the name of the file and the string "write"

    I get this result:
    - Exception IOException caught when I try to write the object.

    Conclusion:
    -You cannot write an object like this or am I missing something?

    Java Code:
    public class Test implements Serializable
    {
       public int number;
       private Serialization s;
    
       public Test(Serialization s) 
       {
          this.s = s;
          number = 4;
       }
    
       public void write(String fileName) {
          
           try {
                ObjectOutputStream out = new ObjectOutputStream(new    FileOutputStream(fileName));
                out.writeObject(this);
                out.close();
            } catch (IOException e) {
                System.out.println("Error!");
            }
        }
    
       public Test read(String fileName) {
    
           Test object=null;
           
           try {
               ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
               object=(Test)in.readObject();
               in.close();
            } catch (IOException e) {
                System.out.println("Error!");
            } catch (ClassNotFoundException ex) {
                System.out.println("Error!");
            }
            
            return object;
        }
    }
    Java Code:
    public class Serialization
    {
       Test test;
    
       public Serialization(String fileName, String mode) {
    
           if (mode.equals("write")) {
                test = new Test(this);
                test.write(fileName);
            } else if (mode.equals("read")) {
                test=test.read(fileName);
                System.out.println(test.number);
            }
        }
    
    }
    Last edited by sky; 11-22-2009 at 07:31 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There's no main method here.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Better change this:

    Java Code:
    } catch (IOException e) {
       System.out.println("Error!");
    }

    ... to this:


    Java Code:
    } catch (IOException e) {
       e.printStackTrace();
    }
    ... and tell us what the stack trace was.

    kind regards,

    Jos

  9. #9
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    There are not main method because I am executing the project with BlueJ, but a new class like this can be added:

    Java Code:
    public class Main
    {
        public static void main (String[] args) {
        
            Serialization s = new Serialization(args[0], args[1]);
           
        }
    
    }
    Stack trace:

    Java Code:
    java.io.NotSerializableException: Serialization
    	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    	at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    	at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    	at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    	at Test.write(Test.java:18)
    	at Serialization.<init>(Serialization.java:10)
    	at __SHELL4.run(__SHELL4.java:14)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:814)
    Last edited by sky; 11-22-2009 at 08:02 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sky View Post
    Stack trace:

    Java Code:
    java.io.NotSerializableException: Serialization
    	at java.io.ObjectOutputStream.writeObject0

    Well, that first line says it all. Fix it and you're in business.

    kind regards,

    Jos

  11. #11
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    What exactly those lines mean? The object Test is not serializable? In that case I don't understand why, because it's implemented as "serializable".

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sky View Post
    What exactly those lines mean? The object Test is not serializable? In that case I don't understand why, because it's implemented as "serializable".
    True, but your Serialization class isn't Serializable and you have a member variable of that class in your Test class; the Serializable framework wants to serialize that variable but it can't.

    In order to make a class Serializable it has to implement that interface plus all types of the member variables also have to be serializable or have to be declared transient.

    kind regards,

    Jos

  13. #13
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    EDIT: Everything seems to work fine with the following code:

    Java Code:
    import java.io.*;
    
    public class Test implements Serializable
    {
       public int number;
    
       public Test() 
       {
          number=0;
       }
    
       public void write(String fileName) {
          
           number = 4;
           
           try {
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
                out.writeObject(this);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
       public Test read(String fileName) {
    
           Test object=null;
           
           try {
               ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
               object=(Test)in.readObject();
               in.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            
            return object;
        }
    }
    Java Code:
    public class Serialization
    {
        private Test test;
       
        public Serialization(String fileName, String mode) {
            
            test = new Test();
            
            if (mode.equals("write")) {
                test.write(fileName);
            } else if (mode.equals("read")) {
                test=test.read(fileName);;
                System.out.println(test.number);
            }
        }
    
    }
    Java Code:
    public class Main
    {
    
        public static void main (String[] args) {
        
            Serialization s = new Serialization(args[0], args[1]);
           
        }
    
    }
    Last edited by sky; 11-23-2009 at 01:29 AM.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sky View Post
    EDIT: Everything seems to work fine with the following code:
    Sure that works because you removed the Serialization object from your Test object (the Serialization class isn't Serializable).

    kind regards,

    Jos

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default

    Put simply, for something to be serializable, all it's non-transient fields must be serializable...that is must "implement Serializable". If they are't then you get that exception.

    Think about it. You've said "I want Test to be serializable", which implies that you want all the fields (non-transient ones that is) to be serialised. Which means that they themselves have to be serializable.

  16. #16
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Yes, that's right. I get a little confusing at the beginning because Java classes as ArrayList are already serializable, and I thought that I only have to write it in the class I'm serializing directly. However, there was also another problem with the code: the line test = new Test(this) should be out of the if, otherwise a NullPointerException is thrown when trying to access the object Test. Thank you for the quick help.

Similar Threads

  1. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  2. Serialization
    By vijay24805 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-10-2009, 09:16 PM
  3. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 09:13 AM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •