This is the mail archive of the java-patches@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: String.toString()


String.toString() needs to throw a NullPointerException if it is called
on a null reference. Thanks to Anthony for pointing out the problem. I
have committed this to the trunk.

Test case:

public class NullPointer
{
  public static void main(String args[])
    {
      try
        {
          String f = null;
          System.out.println(f.toString());
          System.out.println("This should not happen!");
        }
       catch (NullPointerException x)
         {
           System.out.println("Exception generated, Good");
         }
    }
}

regards

  [ bryce ]

1999-07-01  Bryce McKinlay  <bryce@albatross.co.nz>

        * java/lang/String.java (toString): Check for this == null and
throw
        NullPointerException.

The patch:

Index: java/lang/String.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/lang/String.java,v
retrieving revision 1.2
diff -u -r1.2 String.java
--- String.java 1999/04/21 12:12:39     1.2
+++ String.java 1999/07/02 02:50:10
@@ -115,6 +115,9 @@

   public String toString ()
   {
+    // because String is final, we actually get this far on a null
reference
+    if (this == null)
+      throw new NullPointerException();
     return this;
   }



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]