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.
public class NullnessExample { public static void main(String[] args) { Object myObject = null; System.out.println(myObject.toString()); } }
To run the Nullness Checker, simply pass -processor with
checkers.nullness.NullnessChecker. Note:
javacheck
should be an alias to the Checker Framework compiler. See the
manual for an explanation.
javacheck -processor checkers.nullness.NullnessChecker NullnessExample.java
The following error will be produced.
NullnessExample.java:4: error: dereference of possibly-null reference myObject System.out.println(myObject.toString());
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()); } }
No errors should be produced.
javacheck -processor checkers.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.