Inspectopedia 2024.3 Help

'null' assignment

Reports variables that are assigned to null outside a declaration.

The main purpose of null in Java is to denote uninitialized reference variables. In rare cases, assigning a variable explicitly to null is useful to aid garbage collection. However, using null to denote a missing, not specified, or invalid value or a not found element is considered bad practice and may make your code more prone to NullPointerExceptions. Instead, consider defining a sentinel object with the intended semantics or use library types like Optional to denote the absence of a value.

Example:

Integer convert(String s) { Integer value; try { value = Integer.parseInt(s); } catch (NumberFormatException e) { // Warning: null is used to denote an 'invalid value' value = null; } return value; }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

AssignmentToNull
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Assignment issues

Use the Ignore assignments to fields option to ignore assignments to fields.

Inspection options

Here you can find the description of settings available for the 'null' assignment inspection, and the reference of their default values.

Ignore assignments to fields

Not selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.3, Qodana for JVM 2024.3,

Can be installed with plugin:

Java, 243.23126

Last modified: 03 December 2024