Skip to main content
Ben Nadel at Ellen's Stardust Diner (New York City) with: Colin Silverberg and Carol Loffelmann and Daniel Silverberg
Ben Nadel at Ellen's Stardust Diner (New York City) with: Colin Silverberg Carol Loffelmann Daniel Silverberg

Destroying ColdFusion Variables Using JavaCast()

By
Published in Comments (5)

Not really that interesting, but you can destroy variables by setting them equal to the result of a JavaCast() to "null". I have beeing some testing with null values in ColdFusion lately and I did this for a goof:

<!--- Create the objFoo variable. --->
<cfset objFoo = StructNew() />
<cfset objFoo.Bar = "Well Played!" />

<!--- Destroy struct via JavaCast(). --->
<cfset objFoo = JavaCast( "null", 0 ) />

<!--- Dump out variable. --->
<cfdump var="#objFoo#" />

This throws the error:

Variable OBJFOO is undefined.

Now, I am not recommending this, nor should this ever be a replacement for the ColdFusion function StructDelete(). In fact, if you try to use this to destroy a structure element, it doesn't "really" work. Let's rework the code to destroy the structure key, not the structure:

<!--- Create the objFoo variable. --->
<cfset objFoo = StructNew() />
<cfset objFoo.Bar = "Well Played!" />

<!--- Destroy key via JavaCast(). --->
<cfset objFoo.Bar = JavaCast( "null", 0 ) />

<!--- Dump out variable. --->
<cfdump var="#objFoo#" />

This time, when you dump out the structure, "Bar" is still a key in the structure:

undefined struct element

However, its value is "[undefined struct element]" and any attempt to reference it directly will throw the error:

Element BAR is undefined in OBJFOO.

Interesting stuff.

Want to use code from this post? Check out the license.

Reader Comments

3 Comments

LOL "not really interesting"? This is extremely interesting if you use isdefined() to test for the existence of a single session variable and you want to destroy that one var without clearing the whole session scope!

10 Comments

This helped hugely. @cfjedimaster pointed me in the Javacast direction, then i found this from @bennadel.

I have a QoQ running after drawing out the data from an XML file.

My empty variables of "" were not working and throwing a Null Pointer error with no other information and hard to debug.

This is how i fixed it:

<cfset temp = QuerySetCell(myquery, "listing_field1", JavaCast( "null", 0 ), #i#)>

Sweet!

15,848 Comments

@Brien,

You can always use structDelete() to clear variables as well (which might be a little more self-documenting).

@Leigh,

Very nice! Query of Queries is awesome; but, they can really throw you through a loop when dealing with type-casting. I've learned to always always always use javaCast() when manually setting query values.

You might want to check out this post:

www.bennadel.com/blog/1820-Maintaining-ColdFusion-Query-Data-Type-Integrity-Throughout-The-Serialization-Life-Cycle.htm

It specifically talks about maintaining proper query cell values when deserializing from XML (WDDX).

1 Comments

Am using coldfusion 5 and the first piece of code did not throw error - "Variable OBJFOO is undefined.".
What am looking for is a solution to memory leak issue and so am wanting that all the variables should be killed at the end of the code. Any suggestions ?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel