Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization Handing

 
Ma JianHui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50266
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic