aspose file tools*
The moose likes Beginning Java and the fly likes Serialization Handing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialization Handing" Watch "Serialization Handing" New topic
Author

Serialization Handing

Ma JianHui
Greenhorn

Joined: Mar 01, 2008
Posts: 6
import java.io.*;

public class TestSerilization {

public static void main(String[] args) {
Cat c = new Cat(3);
Dog d = new Dog(c,4);
p("before: "+d.getCat().getI());

FileOutputStream fs = null;

try {
fs = new FileOutputStream("D:/HanJia/seria.ser");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fis= null;
try {
fis = new FileInputStream("D:/HanJia/seria.ser");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(fis);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
d = (Dog)ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

p("after: "+d.getCat().getI());

}

static void p(Object o) {
System.out.println(o);
}
}


import java.io.Serializable;

public class Dog implements Serializable{
transient private Cat c;
int i;
public Dog(Cat c,int i) {
this.c = c;
this.i = i;
}

public Cat getCat() {
return c;
}
}


import java.io.Serializable;

public class Dog implements Serializable{
transient private Cat c;
int i;
public Dog(Cat c,int i) {
this.c = c;
this.i = i;
}

public Cat getCat() {
return c;
}
}


The variable c has been declared transient,when runing it,it has a NullPointerException,why?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Do you mean "Why is c null after serialization?" Because it is declared transient. Anything "transient" is missed out on serialization, and on de-serialization there is no value. So it is null.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

In class Dog the object of class Cat is transient


Now, a transient keyword means that whenver you save an object state then the state of the transient variable is not saved.

So, in the program when you retrieve the dog object from the file


Then, this Dog object doen't have the value of Cat and thus, the value is set to null by default.

So when you say

As, the Cat object returned is null and so you get the nullpointerexception.

Hope that solves your query.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Campbell Ritchie:
Do you mean "Why is c null after serialization?" Because it is declared transient. Anything "transient" is missed out on serialization, and on de-serialization there is no value. So it is null.


Well, your query had been already answered, but, probably I was in the process of replying.

By the way, please avoid posting questions twice i.e. in multiple threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization Handing