You use notNull in OCL to test whether a value is present; it is for anyone writing an expression, constraint, or action that must handle missing attribute or variable values.
Syntax
value.notNull
notNull returns a Boolean value:
| Expression result | notNull returns
|
|---|---|
| The value has a value | true
|
| The value is null | false
|
Check an attribute
Given a Car class with a Name attribute, use notNull to test whether Name has been assigned a value:
self.Name.notNull
The expression returns true when Name has a value. It returns false when Name is null.
For example, use the test before an expression that depends on Name being present:
if self.Name.notNull then
self.Name
else
'No name assigned'
endif
Check a variable
You can also test a variable before using it:
vCar.notNull
This returns true when vCar refers to a value and false when it is null.
Use notNull for comparison, not nullValue
Use notNull when you need to determine whether a value is null. Do not compare a value directly to <Type>.nullValue.
vCar.notNull
Use nullValue when you need a typed null value for an assignment or to return null from a method. For example, it can unset an attribute; it is not the null-comparison mechanism.
notNull and notEmpty are different
notNull checks whether one value is null. It does not test whether a collection contains items.
Use notEmpty only when you need to determine whether a collection has at least one element. Do not use ->notEmpty on a value type: the value is converted to a one-item list, causing the test to evaluate to true.
Inverse operator
isNull is the inverse of notNull:
| Value state | value.notNull
|
value.isNull
|
|---|---|---|
| Value is present | true
|
false
|
| Value is null | false
|
true
|
