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

DataOutputStream.writeUTF(null) should not throw NPE

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P5 P5
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      If you call dataoutputstream.writeUTF(string) and that string happens to be null, you get a NPE. special measures have to be taken to check for null values. it would be nicer if writeUTF() handles null strings more gracefully.

      In the example code below, if the String 'comment' happens to be null, the write1() method will throw a NPE. I would have to do something like write2() to account for null values.

      JUSTIFICATION :
      This will simplify user code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When implementing this RFE, care should be taken to differntiate between NULL and EMPTY "" strings... ie:

      a readUTF() corresponding to a writeUF(null) should return null.
      and a readUTF() corresponding to a writeUTF("") should return "".



      ---------- BEGIN SOURCE ----------
        import java.io.*;
        class Person {
          int id;
          String fname, mname, lname;
          String comment;
          
          void write1(DataOutputStream out) {
            out.writeInt(id);
            //...
            out.writeUTF(comment);
          }//write1
          Person read1(DataInputStream in) {
            id = in.readInt();
            //...
            comment = in.readUTF();
          }//read1
          
          public static final String NULL_HOLDER = "NuLl_HoLdEr";
          void write2(DataOutputStream out) {
            out.writeInt(id);
            //...
            if (comment == null)
              out.writeUTF(NULL_HOLDER);
            else
              out.writeUTF(comment);
          }//write2
          Person read2(DataInputStream in) {
            id = in.readInt();
            //...
            comment = in.readUTF();
            if (NULL_HOLDER.equals(comment))
              comment = null;
          }//read2
        }//class

      ---------- END SOURCE ----------

            iris Iris Clark
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: