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
   1234
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 javascript misc model-glue off topic personal project learn slidesix sql

Recent Comments

Chinese Birth Calendar Accuracy Test
mama to be said: ok so i will be 2 months shy of 18 when i have my baby. this calendar does not technically work for ... [More]

Fixing 'User Profile Service Failed The Logon' on Vista
Mike said: That fix worked although all i did was remove .bak and reset state to 0. User was able to log in to... [More]

Chinese Birth Calendar Accuracy Test
Melissa said: Wrong for my daughter, which it predicted to be a boy... we'll see for #2. Predicts a girl (maybe, f... [More]

Adding Auto Generated Code Downloads to BlogCFC
fweerw said: http://www.ibiblio.org/st... http://www.cambodia.ait.a...... [More]

Check Out The New SlideSix
Todd Sharp said: Thanks for the feedback Ben & Rachel! I'll keep it all in mind as I tweak things over the next ... [More]

RSS


adobe community experts

coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com