-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNullMaybe.qhelp
More file actions
53 lines (44 loc) · 1.53 KB
/
NullMaybe.qhelp
File metadata and controls
53 lines (44 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>If a variable is dereferenced, and the variable may have a <code>null</code>
value on some execution paths leading to the dereferencing, the dereferencing
may result in a <code>NullPointerException</code>.
</p>
<p>
A variable may also be implicitly dereferenced if its type is a boxed primitive
type, and the variable occurs in a context in which implicit unboxing occurs.
Note that the conditional operator unboxes its second and third operands when
one of them is a primitive type and the other is the corresponding boxed type.
</p>
</overview>
<recommendation>
<p>Ensure that the variable does not have a <code>null</code> value when it is dereferenced.
</p>
</recommendation>
<example>
<p>
In the following example, the use of the conditional operator causes implicit
unboxing, since the integer literal has type <code>int</code>.
If the parameter <code>p</code> is ever
<code>null</code> then a <code>NullPointerException</code> will occur.
</p>
<sample src="NullMaybe.java" />
<p>
If the implicit unboxing is unintentional, it can be prevented by making sure
that both branches of the conditional operator have the same type.
</p>
</example>
<references>
<li>
The Java Tutorials:
<a href="https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html">Autoboxing and Unboxing</a>.
</li>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.25">Conditional Operator ? :</a>.
</li>
</references>
</qhelp>