Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4065313

One cannot replace an object with a null value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.1.5
    • 1.1.3
    • core-libs
    • None
    • 1.1.5
    • sparc
    • solaris_2.5.1
    • Verified



        Name: rlT66838 Date: 07/17/97


        Altough the documentation mentions that one can replace
        an object with the null value in the replaceObject method
        of ObjectOutputStream, doing this always results in a
        NullPointerException at ObjectOutputStream.writeObject(Object)

        Here is some sample code :
        _______________________________________________________
        import java.io.*;

        class A { }

        class MyObjectOutputStream extends ObjectOutputStream {
          public MyObjectOutputStream(OutputStream out) throws IOException {
            super(out);
            enableReplaceObject(true);
          }
          protected Object replaceObject(Object obj) throws IOException {
            if(obj instanceof A) return null;
            else return obj;
          }
        }

        class Main {
          public static void main(String argv[]) {
            try {
              A a = new A();
            
              MyObjectOutputStream out = new MyObjectOutputStream(new FileOutputStream("test"));
              out.writeObject(a);
              out.flush();
              out.close();
            } catch(Exception e) {
              e.printStackTrace();
            }
          }
        }

        ======================================================================

              jfialli Joe Fialli
              rlewis Roger Lewis (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: