You should not be using null
in Scala at all, and if you don't, it's not necessary to use the NotNull
trait.
If you have values or variables which can have "no value", use the Option
type instead of the null
value. Option
has two subclasses: Some
and None
.
// text is "None", which means it has no value
var text: Option[String] = None
// Use "Some" when it should have a value
text = Some("Hello World")
Option
has lots of useful methods; it can (more or less) be treated as a collection that has zero or one elements, so you can call common collection methods on it and you can use it with pattern matching.
text match {
case Some(s) => println("Text: " + s)
case None => println("Empty")
}