NullPointerException thrown at runtime is one of the most common causes for failure of Java programs.
The Null Analysis tool can detect such programming errors (misuse of potential
null Java values) at compile-time.
The following example of code shows a typical Null Analysis error detection in MicroEJ SDK.
The Null Analysis tool is based on Java annotations. Each Java field, method parameter and method return value must be marked to indicate whether it can be
null or not.
Once the Java code is annotated, module projects must be configured to enable Null Analysis detection in MicroEJ SDK.
Java Code Annotation
MicroEJ defines its own annotations:
- @NonNullByDefault: Indicates that all fields, method return values or parameters can never be null in the annotated package or type. This rule can be overridden on each element by using the Nullable annotation.
- @Nullable: Indicates that a field, local variable, method return value or parameter can be null.
- @NonNull: Indicates that a field, local variable, method return value or parameter can never be null.
MicroEJ recommends to annotate the Java code as follows:
In each Java package, create a
package-info.javafile and annotate the Java package with
@NonNullByDefault. This is a common good practice to deal with non
nullelements by default to avoid undesired NullPointerException. It enforces the behavior which is already widely outlined in Java coding rules.
In each Java type, annotate all fields, methods return values and parameters that can be null with
@Nullable. Usually, this information is already available as textual information in the field or method Javadoc comment. The following example of code shows where annotations must be placed:
Module Project Configuration
To enable the Null Analysis tool, a module project must be configured as follows:
In the Package Explorer, right-click on the module project and select Properties,
Navigate to Java Compiler > Errors/Warnings,
In the Null analysis section, configure options as follows:
Click on the Configure… link to configure MicroEJ annotations:
In the Annotations section, check Suppress optional errors with ‘@SuppressWarnings’ option:
This option allows to fully ignore Null Analysis errors in advanced cases using
If you have multiple projects to configure, you can then copy the content of the
.settings folder to an other module project.
You may lose information if your target module project already has custom parameterization or if it was created with another MicroEJ SDK version. In case of any doubt, please configure the options manually or merge with a text file comparator.
For the benefit of Null Analysis, some APIs have been slightly constrained compared to the Javadoc description. Here are some examples to illustrate the philosophy:
- System.getProperty(String key, String def) does not accept a
nulldefault value, which allows to ensure the returned value is always non
- Collections of the Java Collections Framework that can hold
nullelements (e.g. HashMap) do not accept
nullelements. This allows APIs to return
null(e.g. HashMap.get(Object)) only when an element is not contained in the collection.
Implementations are left unchanged and still comply with the Javadoc description whether the Null Analysis is enabled or not. So if these additional constraints are not acceptable for your project, please disable Null Analysis.
For more information about Null Analysis and inter-procedural analysis, please visit Eclipse JDT Null Analysis documentation.