Files
openide/java/java-impl/resources/inspectionDescriptions/AssignmentToNull.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

29 lines
1.1 KiB
HTML

<html>
<body>
Reports variables that are assigned to <code>null</code> outside a declaration.
<p>The main purpose of <code>null</code> in Java is to denote uninitialized
reference variables. In rare cases, assigning a variable explicitly to <code>null</code>
is useful to aid garbage collection. However, using <code>null</code> 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 <code>NullPointerExceptions</code>.
Instead, consider defining a sentinel object with the intended semantics
or use library types like <code>Optional</code> to denote the absence of a value.</p>
<p><b>Example:</b></p>
<pre><code>
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;
}
</code></pre>
<!-- tooltip end -->
<p>
Use the <b>Ignore assignments to fields</b> option to ignore assignments to fields.
</p>
</body>
</html>