Checker Framework Tutorial - Getting Started - Command Line

# Checker Framework Tutorial

## Getting Started

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

#### Outline

1. Manually spot the null pointer exception
2. Run the Nullness Checker to see how it can catch this error
3. Correct the error
4. Run the Nullness Checker to verify that there are no more errors

#### 1. Spot the null pointer exception

Begin by viewing 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

Run the Nullness Checker to see how it can warn you about this error at compile time.

To run the Nullness Checker, run javac with command-line arguments `-processor org.checkerframework.checker.nullness.NullnessChecker`, as follows.

(Note: In this tutorial, the commands that you cut-and-paste to run on the command line appear in bold after a `$` prompt.)
(Note: You should have already made `javacheck` an alias to the Checker Framework compiler.)

    $ 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

Edit the code to initialize the `myObject` variable 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

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

No errors should be produced.

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

## Next, try Validating User Input, an example using the Regex Checker.