Returning NULL Values From A ColdFusion User Defined Function

Posted September 10, 2007 at 7:00 AM

Tags: ColdFusion

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():

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

NO

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.

Download Code Snippet ZIP File

Comments (4)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


Related Blog Entries



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

That's "null" btw, not NULL. Java is case sensitive and keywords are all lowercase.

The concept of null in Java is that it is 'nothing'. It's a primitive type that's only equal to itself (unlike SQL) and it's not something you can call methods on or use for much of anything but representing 'nothing'.

In CF however, null is really dangerous. You can't compare it to anything else, you can't pass it to CF functions, you can't assign it to some variables, and all kinds of other problems.

Pretty much all you can do with null in CF is get it to pass to Java methods or check if something is null using isDefined().

As for Java, java doesn't use null for loop ending. There's a method called hasNext() or hasMoreElements() or some such other iterator method that tells you if there is more. the next() or nextElement() methods return the next element. It's just a last resort to return null in the even there's no more left (most methods don't actually do this) and you call one of these methods, and even then that's not always true.

In fact methods like next() on the Iterator interface throw NoSuchElementException exceptions when you ask for another element and there's none left.

Did you mean something else for 'loop conditions' ?

Posted by Elliott Sprehn on Sep 11, 2007 at 11:01 PM


@Elliott,

Sorry, you are correct. I was not clear. I didn't mean "looping" in general. I meant just what you said - that some methods in Java return null when they run out of elements. Take for example the LineNumberReader. A some point, the call to ReadLine() will return null, which will then "delete" the variable into which it is stored; I then have to check for that key existence, etc. I use this most generally in a "looping" context.

So, when I said "looping" I meant that that is the situation in which this mostly comes up; I didn't mean to say that that is how Java performs loops in general. Sorry for the confusion.

Posted by Ben Nadel on Sep 12, 2007 at 8:35 AM


You may know this - but I only discovered it a couple of weeks ago...

The same behavior (deleting the variable) is also exhibited iif you have the following within a cffunction:

<cfreturn>

The return type of the function needs to be "Any" (or "Void" - but it it was void, then there would be no need to check it)

Posted by Dan Lancelot on Oct 7, 2007 at 6:28 PM


@Dan,

I didn't know that. Thanks for the tip.

Posted by Ben Nadel on Oct 8, 2007 at 7:12 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting