Results 1 to 8 of 8
Thread: Serializing
- 03-29-2010, 01:44 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
Serializing
hey I have a doubt if I serialize an object of class Dog say d
and close the ObjectoutputStream .close();then i nullify the d refrence variable
and read in d as
try
{
FileOutputStream fos=new FileOutputStream("serial.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(d);
os.close();
}
catch (Exception ioe)
{ ioe.printStackTrace(); }
d=null;//as d is written it should be freed now
try
{
FileInputStream fis =new FileInputStream("serial.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();//using the refrence again.
ois.close();
System.out.println((d.getCollar()).getSize());
}
catch(Exception e)
{
e.printStackTrace();
}
why does this give null pointer exception according to serialization concept the the readObject function retrieves the saved object then whats the problem in now assigning it to new reference variable.
- 03-29-2010, 02:05 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Use sysout to print out the values of d AND d.getCollar before calling (d.getCollar()).getSize().
- 03-29-2010, 02:09 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
thats what i get
FileInputStream fis =new FileInputStream("serial.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
d=(Dog)ois.readObject();
ois.close();
System.out.println(d);
System.out.println((d.getCollar()).getSize());
it got
Dog@de6ced
java.lang.NullPointerException
at test030.main(test030.java:58)
Exception in thread "main" java.lang.NullPointerException
at test030.main(test030.java:66)
- 03-29-2010, 04:03 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
They did say "d.getCollar()" as well...
It appears that is null.
ETA: Also check whether getCollar() returns a null before serializing the object...
- 03-29-2010, 04:19 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 03-29-2010, 04:31 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Well, yes.
I assumed they were assuming it had a collar to begin with...and not that it would magically gain one through the power of serialization.
By the Power of Serlialization!
**Ka-BOOM**
:)
- 03-29-2010, 04:46 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
[QUOTE=Tolls;112014]Well, yes.
I assumed they were assuming it had a collar to begin with...and not that it would magically gain one through the power of serialization.
Well, I assumed that you assumed that they assumed ... oh, forget all about it; the object was successfully de/serialized while the dog didn't have a collar; problem solved ;-)
kind regards,
Jos
- 03-29-2010, 04:47 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
serializing pdf files
By windie in forum New To JavaReplies: 0Last Post: 03-11-2009, 04:46 PM -
Object not serializing
By MamboBanda in forum New To JavaReplies: 1Last Post: 08-12-2008, 12:15 AM -
Serializing Arrays
By javaplus in forum New To JavaReplies: 3Last Post: 11-08-2007, 09:09 AM -
How do I omit an object when serializing?
By Hasan in forum Advanced JavaReplies: 1Last Post: 05-31-2007, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks