Return of 'null'
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.
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.
ReturnOfNull- 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.
Use the following options to configure the inspection:
Whether to ignore
privatemethods. This will also ignore return ofnullfrom anonymous classes and lambdas.Whether
nullvalues on array returns, collection object returns, plain object returns, or a combination of the three should be reported. Return ofnullin methods with return typejava.util.Optionalare always reported.Click Configure annotations to specify which annotations should be considered 'nullable'.
Inspection options
Here you can find the description of settings available for the Return of 'null' inspection, and the reference of their default values.
- Ignore 'private' methods, anonymous classes & lambda's
Default value:
Not selected- Report methods that return arrays
Default value:
Selected- Report methods that return collection objects
Default value:
Selected- Report methods that return objects
Default value:
Selected
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |