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() {
        StdOut.println("here");
        s = "hello";
    }

    public String toString() {
        return s.toUpperCase();
    }

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


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.