For those of you have worked with any of the underlying Java methods in ColdFusion or worked with the installed Java libraries, you probably know that Java returns NULL values a lot of the time. And, furthermore, you'll know that if a NULL value is returned, ColdFusion deals with this event by destroying the variable into which the NULL value is stored. Once you understand this, it makes dealing with Java a lot easier.
Of course, up until now, I have only ever deal with NULL values in terms of Java methods. Well what about in ColdFusion; can I return a NULL value from a ColdFusion user defined function? And if so, does it work in the same way? To test this, I set up a simple ColdFusion user defined function which returns a NULL value using JavaCast():
<cffunction name="GetNull" access="public" returntype="any" output="false" hint="Returns a NULL value."> <!--- Return a Java null. ---> <cfreturn JavaCast( "null", 0 ) /> </cffunction>
Now, I am gonna see what happens when I store that returned value into a ColdFusion struct:
<!--- Create a Data Struct. ---> <cfset objData = StructNew() /> <!--- Store NULL value. ---> <cfset objData.Null = GetNull() /> <!--- Test for KEY existence. ---> #StructKeyExists( objData, "Null" )#
Running the code above, we get the following output:
Cool. It looks like ColdFusion handles NULL values in exactly the same way whether it was returned from Java method or a ColdFusion user defined function. I am not sure if I would ever use this, but, I have to say that I do sort of like the way Java returns NULL values when it ends things like loop conditions. It might be worth looking into making that a ColdFusion standard as well.
Want to use code from this post? Check out the license.