NullException
NullException()
An attempted access to a Nullable with no defined value.
Examples
In the Julia programming language, the function NullException() is used to handle attempted access to a Nullable with no defined value. It throws an exception when there is an attempt to access the value of a Nullable object that is set to null.
Common examples of its use:
-
Handle
Nullableaccess attempt:julia> x = Nullable{Int}(null); julia> try value = x[] catch ex if ex isa NullException println("Error: Nullable value is null.") end end Error: Nullable value is null.In this example,
xis aNullableobject with no defined value. Thetry-catchblock is used to catch theNullExceptionand handle the error appropriately. - Avoid accessing
Nullablevalue without checking:julia> y = Nullable{String}("Hello"); julia> if isnull(y) println("Value is null.") else println("Value is: ", y[]) end Value is: HelloThis example checks if the
Nullablevalue is null using theisnullfunction before accessing the value usingy[]. It prevents a potentialNullExceptionby ensuring that the value is not null before accessing it.
Common mistake example:
julia> z = Nullable{Float64}(0.0);
julia> value = z[]
ERROR: NullException: NullException()
In this example, the NullPointerException is thrown because the z object is set to null. It's crucial to check for null values before attempting to access the value to avoid this error.
See Also
BigFloat, BigInt, Dict, eltype, fieldtype, Float32, Float64, IntSet, isa, isalnum, isalpha, isascii, iseltype, isequal, isgraph, isimmutable, isinteractive, isleaftype, isnull, ispunct, isspace, issubtype, keytype, Nullable, NullException, promote_type, typeintersect, typejoin, typemax, typemin, typeof, Val, valtype,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.