Copy link to clipboard
Copied
<cfscript>
test=null ///Variable NULL is undefined
test1=javaCast("null","");
writeDump(test1); // Variable test1 is undefined
</cfscript>
Is there a way to make prevent getting the "variable null is undefined" error? If not, then I guess javacast("null", "") is the only alternative. However, when trying to use test1, I get the error "test1 is undefined". Could someone suggest a way of getting around this please?
Thank you very much
1 Correct answer
@Junaid32092262a1i8, you haven't said what CF version you're on. Assuming it's cf2018 or above, some good news is that DOES have null support--though it must be enabled. It is not, by default (so you'd get that error until you do enable it).
You can enable it in the cf admin (first settings page), or in your application (either this.enablenullsupport=yes in application.cfc or as an attribute of cfapplication in application.cfm).
The feature (and setting it) is described in the docs:
...Copy link to clipboard
Copied
@Junaid32092262a1i8, you haven't said what CF version you're on. Assuming it's cf2018 or above, some good news is that DOES have null support--though it must be enabled. It is not, by default (so you'd get that error until you do enable it).
You can enable it in the cf admin (first settings page), or in your application (either this.enablenullsupport=yes in application.cfc or as an attribute of cfapplication in application.cfm).
The feature (and setting it) is described in the docs:
And also in an Adobe blog post from 2018 (which has some quirks)
https://coldfusion.adobe.com/2018/07/null-support-in-coldfusion-2018
Neither makes clear that you also must cause cf to recompile the program using the null, for this change of behavior to take effect. You can cause that simply by editing the file i(n any way: add and remove a space, for instance), then save it and re-run the request.
(Technically the docs mention it, but elsewhere on that page, about how changes affecting datatype are indeed at compile time--which can be changed via a JVM arg at cf startup.)
Let us know how it goes.
/Charlie (troubleshooter, carehart.org)
Copy link to clipboard
Copied
You get an error with
test=null
because, as mentioned in the documentation shared by Charlie, you haven't configured enablenullsupport=true in ColdFusion. The default is enablenullsupport=false.
When enablenullsupport is false, then null is just another name denoting a variable. But the variable does not exist. Which is why you get the error message.
When enablenullsupport is true, then ColdFusion automatically converts the word null on the right-hand-side of the assignment to the null value. I consider this conversion forced and counter-intuitive.
Take the following code. for example.
<cfscript>
try {
nullTest1=hull;
nullTest2=javacast("null", 0);
}
catch (any e) {
//Dump exception
}
</cfscript>
<p>
nullTest1=null;<br>
nullTest2=javacast("null", 0);
<p>
<cfoutput>
<p>
isdefined('variables.nullTest1'): #isdefined('variables.nullTest1')#<br>
isdefined('variables.nullTest2'): #isdefined('variables.nullTest2')#
</p>
isNull(nullTest1): #isNull(nullTest1)#<br>
isNull(nullTest2): #isNull(nullTest2)#
</cfoutput>
Run it in turn with this.enableNullSupport = false; in Application.cfc, then with this.enableNullSupport = true;
With this.enableNullSupport = false;, the result is:
Whereas, with this.enableNullSupport = true; the isNull() test results in the exception "Variable NULLTEST1 is undefined.". This is the kind of behaviour I consider counter-intuitive. Which is why I use this.enableNullSupport = false; in my ColdFusion applications.

