This page walks you through a simple example to show how to use a checker using the command line tools. It shows how the Nullness checker can be used to prevent null pointer exceptions.

Outline

1. Spot the null pointer exception

Begin by opening NullnessExample.java. (If you have not already, download the source files for the tutorial.) It is a simple Java program with an obvious null pointer exception.

public class NullnessExample {
    public static void main(String[] args) {
        Object myObject = null;
        System.out.println(myObject.toString());
    }
}

2. Run the Nullness Checker

To run the Nullness Checker, simply pass -processor with org.checkerframework.checker.nullness.NullnessChecker. Note: javacheck should be an alias to the Checker Framework compiler. See the manual for an explanation.

javacheck -processor org.checkerframework.checker.nullness.NullnessChecker NullnessExample.java

The following error will be produced.

NullnessExample.java:9: error: [dereference.of.nullable] dereference of possibly-null reference myObject
        System.out.println(myObject.toString());
                           ^
1 error

3. Correct the error

Change myObject to some non-null value

public class NullnessExample {
    public static void main(String[] args) {
        Object myObject = new Object();
        System.out.println(myObject.toString());
    }
}

4. Re-run the Nullness Checker

No errors should be produced.

javacheck -processor org.checkerframework.checker.nullness.NullnessChecker NullnessExample.java

This was a very simple example to show how to use the Checker Framework in Eclipse. The next example is a little more complex.