IntelliJ IDEA 2018.1 Help

Infer nullity

The nullability analysis in IntelliJ IDEA can scan your code and add @Nullable and @NotNull annotations to your code. The analysis helps you detect contract violations in your code and suggests null checks where possible.

Run the nullability analysis

  1. From the main menu, select Analyze | Infer Nullity.

    If you haven't added the library with annotations to the classpath, IntelliJ IDEA will prompt you to do it. You can either use annotations from the distribution package or copy them locally.

    If you share your project through VCS, it is recommended that you create a local copy of the library, as you will not be able to share annotations from the distribution package.

  2. In the Specify Infer Nullity Scope dialog, select the scope of the analysis. If you want to include test sources and annotate local variables, select the corresponding checkboxes.
  3. Click OK.

    IntelliJ IDEA adds import statements for annotations if required, and annotates parameters and variables.

Inferred annotations

IntelliJ IDEA scans bytecode from SDKs and libraries and infers the @Nullable and @NotNull annotations automatically. You can use these annotations later to analyze your source code to spot places where you overlooked null.

In the editor, inferred annotations are marked with the external annotation gutter icon gutter icon.

Show inferred annotations inline

You can configure the IDE to show inferred annotations right in your code.

  1. Press Ctrl+Alt+S to open the Settings/Preferences dialog and go to Editor | General | Appearance.
  2. Select the Show inferred annotations inline checkbox.
  3. Apply the changes and close the dialog.
    inferred inline
Last modified: 26 June 2018

See Also