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.
public class NullnessExample { public static void main(String[] args) { Object myObject = null; System.out.println(myObject.toString()); } }
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
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()); } }
$ 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.