This page walks you through a simple example to show how to use a type checker in the Eclipse plugin. It shows how the Nullness Checker can be used to prevent null pointer exceptions.
If you have not already, download the Eclipse projects. Begin by importing the checker-framework-tutorial project into your Eclipse workspace. Select from the menu, File->Import->Existing Project, then browse to the eclipse-projects/checker-framework-tutorial directory.
Open NullnessExample.java. 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());
}
}
Run the Nullness Checker to see how it can warn you about this error, before you run the program.
The following error with be produced.
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()); } }
Re-run the checker. No errors should be produced.
This was a very simple example to show how to use the Checker Framework in Eclipse. The next example is a little more complex.