Null Analysis

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.

Example of Null Analysis Detection

Example of Null Analysis Detection


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 file and annotate the Java package with @NonNullByDefault. This is a common good practice to deal with non null elements 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:



MicroEJ SDK 5.3.0 or higher requires annotations declared in EDC-1.3.3 or higher. See EDC 1.3.3 Changelog for more details.

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:

    • ej.annotation.Nullable
    • ej.annotation.NonNull
    • ej.annotation.NonNullByDefault
  • In the Annotations section, check Suppress optional errors with ‘@SuppressWarnings’ option:


    This option allows to fully ignore Null Analysis errors in advanced cases using @SuppressWarnings("null") annotation.

If you have multiple projects to configure, you can then copy the content of the .settings folder to an other module project.

Null Analysis Settings Folder

Null Analysis Settings Folder


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.

MicroEJ Libraries

Many libraries available on Central Repository are annotated with Null Analysis. If you are using a library which is not yet annotated, please contact our support team.

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 null default value, which allows to ensure the returned value is always non null.
  • Collections of the Java Collections Framework that can hold null elements (e.g. HashMap) do not accept null elements. 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.

Advanced Use

For more information about Null Analysis and inter-procedural analysis, please visit Eclipse JDT Null Analysis documentation.