mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
29 lines
1.1 KiB
HTML
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> |