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.

Full-Null Support

Please note that full null support is NOT the default in BoxLang. 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, 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.bx file, which can be used when building web applications. You can learn more about it here

Application.bx
class{
    this.nullSupport = true;
}

Checking For Nullness

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" ) ){

}

Also, remember that you can use the Elvis operator to test for null and an operator and expression.

results = getMaybeData() ?: "default value"

We would recommend that you use isNull() as it expresses coherently its purpose. Since isDefined() can also evaluate expressions.

Creating Nulls

You can create nulls in different ways in BoxLang. Let's explore these:

ApproachFull NullDescription

null keyword

r = null

Non returning function call

If a function returns nothing, its assignment will produce a null. function getNull(){} r = getNull()

nullValue()

r = nullValue()

javaCast( "null", "" )

r = javaCast( "null", "" )

In Practice

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 have "" which is an empty string. It’s still something.

Null in BoxLang 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 BoxLang to give us the sixth element of that list. There is no sixth element, so BoxLang 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 updated

Logo

Copyright & Register Trademark by Ortus Solutions, Corp & Ortus Software, LLC