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:

  1. Handle Nullable access 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, x is a Nullable object with no defined value. The try-catch block is used to catch the NullException and handle the error appropriately.

  2. Avoid accessing Nullable value without checking:
    julia> y = Nullable{String}("Hello");
    julia> if isnull(y)
              println("Value is null.")
          else
              println("Value is: ", y[])
          end
    Value is: Hello

    This example checks if the Nullable value is null using the isnull function before accessing the value using y[]. It prevents a potential NullException by 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.

*Required Field
Details

Checking you are not a robot: