Inspectopedia 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; }

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

Inspection options

Option

Type

Default

Ignore assignments to fields

Checkbox

false

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023