Null & Nothingness
null does mean something!
What is nothingness? Is there nothingness only in outer space? If a tree falls in the forest and nobody listens, does it make a sound? Starting to see the point? Does nothing really mean nothing? To be or not to be? OK, I think we are going on a philosophical tangent, so let's get back to our geekiness:
null
is Java's way to refer to "nothingness.", something that does not exist and has no value. Support for thenull
keyword itself was introduced as an option in ColdFusion 2018 (as discussed in this blog post) and has existed as an option in Lucee (as discussed in this guide).Please note that full null support is NOT the default in the CFML engines. Meaning you will not be able to use the
null
keyword until it is activated or get real null
values from databases or external services. In reality, you still could simulate null
without full null support in both engines, and sometimes you get an empty string, sometimes a full Java null
. So basically, the nonfull null support is a partial null support, which makes it hard for developers. So as a rule of thumb, we always recommend checking for nullness no matter WHAT!Eventually, this flag should default to true, in our opinion, and offer full-null support out of the box.
Ok, back to activating full-null support. You can do this in the admin or programmatically via the
Application.cfc
file, which can be used when building web applications. You can learn more about it hereApplication.cfc
component{
this.nullSupport = true;
}
Use the
isNull()
or isDefined()
methods to evaluate for nothingness.r = getMaybeData()
if( isNull( r ) ){
// do something because r doesn't exist
}
if( isDefined( "r" ) ){
}
results = getMaybeData() ?: "default value"
We would recommend that you use
isNull()
as it expresses coherently its purpose. Since isDefined()
can also evaluate expressions.You can create nulls in different ways in CFML. Let's explore these:
Approach | Full Null | Description |
---|---|---|
null keyword | r = null | |
Non returning function call | If a function returns nothing, its assignment will produce a null.
function getNull(){}
r = getNull() | |
nullValue() | Lucee only function.
r = nullValue() | |
javaCast( "null", "" ) | Available in all engines r = javaCast( "null", "" ) |
If you have three eggs, and eat three eggs, then you might think you have "nothing," but in terms of eggs you have "0". Zero is something, it’s a number, and it’s not nothing.
If you’re working with words and have a string like "hello" then delete the "h", "e", "l"s, and "o" you might think you’d end up with nothing, but you really have "" which is an empty string. It’s still something.
Null in CFML is usually encountered when you ask for something that doesn’t exist. When looking at arrays, for instance, we created a list with five elements then asked CFML to give us the sixth element of that list. There is no sixth element, so CFML gave us null. It isn’t that there’s a blank in that sixth spot (""), it’s not a number 0, it’s nothingness – null.
Examples
function getData( filter ){
if( isNull( arguments.filter ) ){
// then do this
} else {
// use the filter
}
}
function returnsNull(){
if( key.exists( "invalid" ) ){
return key[ "invalid" ];
}
}
results = returnsNull();
writeOutput( isNull( results ) );
Also note that if a function returns nothing it will be the same as returning
null
.Last modified 3mo ago