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
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
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 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.