Results 1 to 8 of 8

Thread: Serializing

  1. #1
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Use sysout to print out the values of d AND d.getCollar before calling (d.getCollar()).getSize().

  3. #3
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default 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)

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

    Default

    They did say "d.getCollar()" as well...
    It appears that is null.

    ETA: Also check whether getCollar() returns a null before serializing the object...

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

    Default

    Quote Originally Posted by Tolls View Post
    They did say "d.getCollar()" as well...
    It appears that is null.

    ETA: Also check whether getCollar() returns a null before serializing the object...
    It returns null and the dog object isn't null. See the output the OP showed; the dog doesn't have a collar.

    kind regards,

    Jos

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

    Default

    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**
    :)

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

    Default

    [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

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

    Default

    Unless it was a transient collar...

    ...why yes, that is my coat...

Similar Threads

  1. serializing pdf files
    By windie in forum New To Java
    Replies: 0
    Last Post: 03-11-2009, 05:46 PM
  2. Object not serializing
    By MamboBanda in forum New To Java
    Replies: 1
    Last Post: 08-12-2008, 01:15 AM
  3. Serializing Arrays
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 10:09 AM
  4. How do I omit an object when serializing?
    By Hasan in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2007, 05:18 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
  •