Bug1.java


Below is the syntax highlighted version of Bug1.java from §3.2 Creating Data Types.


/*************************************************************************
 *  Compilation:  javac Bug1.java
 *  Execution:    java Bug1
 *
 *  Mistake with constructor since it shouldn't have return type void.
 *  One way to debug this is to insert a println statement in the
 *  constructor, and observe that it never gets executed.
 * 
 *  % java Bug1
 *  Exception in thread "main" java.lang.NullPointerException
 *        at Bug1.main(Bug1.java:24) 
 *
 *************************************************************************/

public class Bug1 {
   private String s;

   public void Bug1() {
       System.out.println("here");
       s = "hello";
   }

   public String toString() { return s;  }

   public static void main(String[] args) {
      Bug1 x = new Bug1();
      System.out.println(x);
   }
}   


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.