
org.sonar.l10n.java.rules.java.S2225.html Maven / Gradle / Ivy
Why is this an issue?
Calling toString()
or clone()
on an object should always return a string or an object. Returning null
instead contravenes the method’s implicit contract.
Noncompliant code example
public String toString () {
if (this.collection.isEmpty()) {
return null; // Noncompliant
} else {
// ...
Compliant solution
public String toString () {
if (this.collection.isEmpty()) {
return "";
} else {
// ...
Resources
- CWE - CWE-476 - NULL Pointer Dereference
- CERT, EXP01-J. - Do not use a null in a case where an object is required
© 2015 - 2025 Weber Informatics LLC | Privacy Policy