Handling Java Nulls In ColdFusion

I'm putting the finishing touches on my PowerPoint utils component when I came across a weird Java error. I've seen it before and it may very well be my lack of Java experience that is causing my lack of understanding here.

Say you have a Java method getFoo() that is expected to return a string. I've found that sometimes ColdFusion doesn't properly handle Java nulls so when you try to set a value you'll get something like so when trying to reference it:

Element foo is undefined in a CFML structure referenced as part of an expression.

ColdFusion cannot determine the line of the template that caused this error. This is often caused by an error in the exception handling subsystem.

It seems that ColdFusion simply chokes on it. I came up with a hacky fix by passing the value to another method which properly returns an empty string:

<cffunction name="fixNull" access="private">
   <cfargument name="valueToFix" default="" />
   <cfset rStr = "" />
   <cfif isDefined("arguments.valueToFix")>
      <cfset rStr = arguments.valueToFix />
   </cfif>
   <cfreturn rStr />
</cffunction>

Seems a bit hacky but it works. If someone can shed a little more light on the subject I'd appreciate it.

Comments
Your understanding is correct...if a ColdFusion variable has its value set by a Java method, and that method returns null, the variable is dereferenced in ColdFusion. It ceases to exist at all, in any scope.

Your function should save you some keystrokes...I've gotten used to just putting if(isDefined("var")) after all my Java calls.
# Posted By rich | 12/15/07 3:37 PM
Ben Nadel had a post or 2 on NULLs lately.

http://www.bennadel.com/blog/117-Handling-NULL-Val...
# Posted By Michael Sharman | 12/15/07 10:29 PM

Calendar

Sun Mon Tue Wed Thu Fri Sat
     12
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex funny stuff misc model-glue off topic personal project learn slidesix sql

Recent Comments

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
todd sharp said: Jenn: Check your email. Todd [More]

ColdFusion/Sharepoint Integration - Part 1 - Authenticating
Jenn said: When does the next issue of FAQU come out? For that matter is it at all possible to get a preview o... [More]

A Major Milestone In My Marriage
Brian Meloche said: I converted my wife last year. That said, I find Firefox 3 a bit crashy. [More]

SlideSix Gets PDF Support
todd sharp said: Do you mean add numbering to the PDF output? If so, yeah, that would be easy. Just to be clear tho... [More]

SlideSix Gets PDF Support
salvatore fusto said: nice work Todd, but can you add slide numbering: 1/N, 2 of N and so long? it would be useful. regard... [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com