
## Review A dedicated reviewer checked the rule description successfully for: - [ ] logical errors and incorrect information - [ ] information gaps and missing content - [ ] text style and tone - [ ] PR summary and labels follow [the guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule) --------- Co-authored-by: Loris S <91723853+loris-s-sonarsource@users.noreply.github.com> Co-authored-by: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com>
75 lines
1.9 KiB
Plaintext
75 lines
1.9 KiB
Plaintext
The variable `myObject` is equal to `null`, meaning it has no value:
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public void method() {
|
|
Object myObject = null;
|
|
System.out.println(myObject.toString()); // Noncompliant: myObject is null
|
|
}
|
|
----
|
|
|
|
The parameter `input` might be `null` as suggested by the `if` condition:
|
|
|
|
[source,java,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
public void method(Object input)
|
|
{
|
|
if (input == null)
|
|
{
|
|
// ...
|
|
}
|
|
System.out.println(input.toString()); // Noncompliant
|
|
}
|
|
----
|
|
|
|
The unboxing triggered in the return statement will throw a `NullPointerException`:
|
|
|
|
[source,java,diff-id=3,diff-type=noncompliant]
|
|
----
|
|
public boolean method() {
|
|
Boolean boxed = null;
|
|
return boxed; // Noncompliant
|
|
}
|
|
----
|
|
|
|
Both `conn` and `stmt` might be `null` in case an exception was thrown in the try{} block:
|
|
|
|
[source,java,diff-id=4,diff-type=noncompliant]
|
|
----
|
|
Connection conn = null;
|
|
Statement stmt = null;
|
|
try {
|
|
conn = DriverManager.getConnection(DB_URL,USER,PASS);
|
|
stmt = conn.createStatement();
|
|
// ...
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
stmt.close(); // Noncompliant
|
|
conn.close(); // Noncompliant
|
|
}
|
|
----
|
|
|
|
As `getName()` is annotated with `@CheckForNull`, there is a risk of `NullPointerException` here:
|
|
|
|
[source,java,diff-id=5,diff-type=noncompliant]
|
|
----
|
|
@CheckForNull
|
|
String getName() {...}
|
|
|
|
public boolean isNameEmpty() {
|
|
return getName().length() == 0; // Noncompliant
|
|
}
|
|
----
|
|
|
|
As `merge(...)` parameter is annotated with `@Nonnull`, passing an identified potential null value (thanks to @CheckForNull) is not safe:
|
|
|
|
[source,java,diff-id=6,diff-type=noncompliant]
|
|
----
|
|
private void merge(@Nonnull Color firstColor, @Nonnull Color secondColor) {...}
|
|
|
|
public void append(@CheckForNull Color color) {
|
|
merge(currentColor, color); // Noncompliant: color should be null-checked because merge(...) doesn't accept nullable parameters
|
|
}
|
|
----
|