Inspectopedia
 
2024.3

Return of 'null'

Warning
New
Last modified: 03 December 2024

Reports return statements with null return values. While occasionally useful, this construct may make the code more prone to failing with a NullPointerException.

If a method is designed to return null, it is suggested to mark it with the @Nullable annotation - such methods will be ignored by this inspection.

Example:

After the quick-fix is applied:

If the return type is java.util.Optional, an additional quick-fix to convert null to Optional.empty() is suggested.

Use the following options to configure the inspection:

  • Whether to ignore private methods. This will also ignore return of null from anonymous classes and lambdas.

  • Whether null values on array returns, collection object returns, plain object returns, or a combination of the three should be reported. Return of null in methods with return type java.util.Optional are always reported.

  • Click Configure annotations to specify which annotations should be considered 'nullable'.