Results 1 to 16 of 16
Thread: Object serialization
- 11-22-2009, 05:48 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
- 11-22-2009, 06:02 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Try it and see what happens.
-
AFAIK, you don't call those methods on the objects themselves, but on the ObjectStreams that read and write the objects.
- 11-22-2009, 06:20 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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:AFAIK, you don't call those methods on the objects themselves, but on the ObjectStreams that read and write the objects.
Java Code:ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName)); out.writeObject(this); out.close();
- 11-22-2009, 06:29 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 11-22-2009, 07:22 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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.
-
There's no main method here.
- 11-22-2009, 07:41 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Better change this:
Java Code:} catch (IOException e) { System.out.println("Error!"); }
... to this:
... and tell us what the stack trace was.Java Code:} catch (IOException e) { e.printStackTrace(); }
kind regards,
Jos
- 11-22-2009, 07:46 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
There are not main method because I am executing the project with BlueJ, but a new class like this can be added:
Stack trace:Java Code:public class Main { public static void main (String[] args) { Serialization s = new Serialization(args[0], args[1]); } }
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.
- 11-22-2009, 07:53 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 11-22-2009, 08:09 PM #11
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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".
- 11-22-2009, 08:13 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-22-2009, 08:25 PM #13
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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.
- 11-23-2009, 07:38 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 11-23-2009, 09:36 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-23-2009, 01:10 PM #16
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
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
-
Serialization
By thayalan in forum Advanced JavaReplies: 4Last Post: 08-03-2009, 10:22 PM -
Serialization
By vijay24805 in forum Threads and SynchronizationReplies: 1Last Post: 04-10-2009, 09:16 PM -
about serialization
By bishnu in forum New To JavaReplies: 0Last Post: 12-19-2008, 09:13 AM -
Need help using serialization
By xcallmejudasx in forum New To JavaReplies: 0Last Post: 12-02-2008, 08:23 PM -
What is Serialization and de-serialization in Java
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks