Returning NULL Values From A ColdFusion User Defined Function

Posted September 10, 2007 at 7:00 AM by Ben Nadel

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

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

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.


You Might Also Be Interested In:



Reader Comments

Sep 11, 2007 at 11:01 PM // reply »
132 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' ?


Sep 12, 2007 at 8:35 AM // reply »
11,243 Comments

@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.


Oct 7, 2007 at 6:28 PM // reply »
5 Comments

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)


Oct 8, 2007 at 7:12 AM // reply »
11,243 Comments

@Dan,

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


Aug 28, 2009 at 10:45 AM // reply »
3 Comments

Hi Ben!
I have a CF-Java codes (not written by me, I don't speak java) that converts a text feed into a query.
All I need to do is to query this query and use the resultset. Unfortunately this java things create difficulty on my simple query may be it is related to what you wrote in this blog about java returning null instead of empty string(?)

My simple query below threw this error:
query to query run time error:
Comparison exception while executing <>.
Unsupported Type Comparison Exception: The <> operator does not support comparison between the following types:
Left hand side expression type = "DOUBLE".
Right hand side expression type = "STRING".

<cfquery name="test" dbtype="query>
Select SSN, Birthdate from MyQuery
Where SSN <> '' OR
Birthdate <> ''
</cfquery>


Sep 2, 2009 at 9:30 AM // reply »
11,243 Comments

@Alec,

What you are running into is one of the many times that ColdFusion query of queries become frustrating. Probably, ColdFusion is either trying to convert SSN numbers OR Birthdays into a numberic value (DOUBLE) such that your string '' is not a valid comparison. Try casting the SSN and bday back to varchar:

<cfquery name="test" dbtype="query>
Select SSN, Birthdate from MyQuery
Where
CAST( SSN AS VARCHAR )<> '' OR
CAST( Birthdate AS VARCHAR ) <> ''
</cfquery>


Jul 31, 2010 at 3:46 PM // reply »
1 Comments

@Ben
I was just looking for the same but in CFMX 6.1
Have you figured out to pass "null" to a Java Constructor or a Method? in CFMX 6.1


Aug 1, 2010 at 7:03 PM // reply »
11,243 Comments

@Khurram,

I haven't been on CFMX in a long time. It looks like javaCast() only added support for NULL values in CF7. Perhaps you can try calling a Java method that returns a NULL value (and pass that to the constructor). Sorry I don't have any better advice.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools