Handling NULL Values In ColdFusion

Posted July 5, 2006 at 3:01 PM

Tags: ColdFusion

There have been some NULL value discussions taking place lately on the House of Fusion CF-Talk list. Null values in ColdFusion can be very tricky. There are null values in a database, which I have discussed before, and there are NULL values as variable data. In my experience, there are no Null values in ColdFusion variable data, and in fact, if you set a variable to NULL (via some non-cf call), the variable itself gets destroyed.

Take as an example, reading in a ZIP file. To do so, you have to keep streaming in zip entries until you hit, in Java, a null value. In Java, you would do something like:

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

  • while (objZipEntry != null){ ... }

In ColdFusion however, putting a null value into objZipEntry will actually destroy it and remove it from its parent variable scope. So, the equivalent in ColdFusion would be:

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

  • // Loop while we did not hit a "null" value.
  • while (StructKeyExists( REQUEST, "ZipEntry" )){
  • // .... more code here ....
  •  
  • // Get the next zip entry (finish if null).
  • REQUEST.ZipEntry = REQUEST.ZipInputStream.GetNextEntry();
  • }

As you can see, we have set the ZipEntry variable into the REQUEST scope (not shown in the code). Then, for each while loop iteration, we get the next zip entry. Once the Java object ZipInputStream returns null, signaling the end of the zip entry streams, ColdFusion "executes" that null value by removing the ZipEntry key from the REQUEST scope.

In order to handle this well, I would suggest scoping all values that might receive null values. You can use the VARIABLES scope, but my personal choice would be REQUEST or some other custom struct.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page





Reader Comments

Mar 19, 2008 at 9:19 AM // reply »
3 Comments

The request scope is slightly dangerous since it's globally available to all objects, methods, etc within the given request. You run some chance of colliding with another variable in that scope, especially in larger applications. A local scope is much safer, this, variables, or a struct you create for this purpose.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »