You use isNull in OCL expressions to test whether a value has no assigned value.
Syntax
expression.isNullThe operator returns a Boolean value:
| Expression result | isNull returns
|
|---|---|
| The expression has no assigned value | true
|
| The expression has an assigned value | false
|
Example
Assume that vCar is a variable holding a Car object and that Name is an attribute of Car.
vCar.Name.isNullThis expression returns true when Name has not been assigned. It returns false when Name contains a value.
You can use the result in a condition. For example, this condition identifies a car whose name is not assigned:
if vCar.Name.isNull then
-- Handle the missing name
endifTest for null; do not compare with nullValue
Use isNull to test whether an expression is null. Do not compare an expression directly with <Type>.nullValue.
-- Correct: tests whether Name is null
vCar.Name.isNull
-- Wrong: do not use nullValue for a comparison
vCar.Name = String.nullValuenullValue is a typed null value for assigning null to an attribute or variable, or for returning null from a method. It is not the null-test operator.
Related operators
Use isNull for a value that may be unassigned. Do not use collection operators to perform this test:
| Need | Use |
|---|---|
| Test whether a value is not assigned | expression.isNull
|
| Test whether a collection contains no elements | isEmpty
|
| Test whether a collection contains one or more elements | notEmpty
|
