Absence modelled with null instead of Option
ID |
scala.correctness.null_usage |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Api Misuse |
Language |
Scala |
Description
Absence is being modelled with null, which the type system does not track, so a missing value surfaces as a NullPointerException far from here. Use Option: wrap a value coming from Java with Option(…), return None instead of null, and test with isEmpty/isDefined or a match. Passing null as an argument to a Java API is not reported.
Rationale
Absence is being modelled with null, which the type system does not track, so a missing value surfaces as a NullPointerException far from here. Use Option: wrap a value coming from Java with Option(…), return None instead of null, and test with isEmpty/isDefined or a match. Passing null as an argument to a Java API is not reported.
The following code illustrates the pattern detected by this rule:
class NullUsage {
// FLAGGED: Absence modelled with null instead of Option
private var cache: String = null
}