Null values in ColdFusion

I have recently been playing around with Java code imported into ColdFusion and ran into errors with undefined/null values. ColdFusion doesn’t really have a concept of null values, ColdFusion has JavaCast( “null”, “” ) for when you want to pass a null into a Java object but what happens when your Java object returns a null? How do you test for it? How do we handle it?

Why use null values anyway?

There are differing opinions when it comes to null values, some people would rather not see a null value anywhere and throw an exception or return something “more meaningful”. When a variable does not exist and it is legal for it to not exist surely nothing could be more explicitly descriptive than a null value as a returntype of a getter for that method. You could however implement a hasProperty() or isPropertyDefined() Boolean method to return, true or false, so then we can wrap our getProperty method in an if statement:

<cfif something.hasProperty() >
    <cfset property = something.getProperty() />
</cfif>

However, this means you have to write an additional method for every method that could otherwise return null from getProperty. Also for shared resources in multithreaded environments this does nothing for us anyway as by time we get to getProperty from hasProperty something somewhere else could have completed a removeProperty call and we are stuck with the same problem, whereas handling it all from a single request to the shared resource is far more ideal.

So the problem is handling this all in ColdFusion. ColdFusion doesn’t like it too much, there are no null comparisons in ColdFusion and if you have the following code:

<cfset var myVar = methodThatReturnsNull() />

and try in anyway to directly access that variable, ColdFusion will throw an error. However, there is at least one method that can determine if a value is undefined or null in ColdFusion, our trusty StructKeyExists method, which means your variable will have to exist within a struct. Now we can do the following:

<cfset var local = {} />
<cfset local.myVar = methodThatReturnsNull() />
<cfif StructKeyExists( local, "myVar" )>
    <!--- Continue processing if not null --->
</cfif>

However, you may notice if you attempted to dump that… WHOAH! An error, myVar in local is undefined?! But you said the key does not exists. So yes not entirely accurate, the myVar key does in fact exist within the local struct but it’s value is undefined, which is what is causing the problems. In order to correct this we call struct delete to clear it:-

<cfset var local = {} />
<cfset local.myVar = methodThatReturnsNull() />
<cfif StructKeyExists( local, "myVar" ) ||
            !StructDelete( local, "myVar" )>
    <!--- Continue processing if not null --->
</cfif>
<cfdump var="#local#" />

There it all dumps fine and you have just handled a null value in ColdFusion

Stuart Wakefield

Software engineer and musician. I like graphic novels, illustration and games. I dabble with digital art and game development.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s