Skip to content
Don Quist edited this page Mar 18, 2014 · 6 revisions

Previous Conditionals


## 10. Nothingness & Null

What is nothingness? Is there nothingness only in outer space? Really, when we think of nothing isn't it just the absence of something? Ok, that's too much philosophy

ColdFusion did not have a way of referring to nothingness until version 9. Prior to this NULL had to be evaluated using IsDefined("variableName"). ColdFusion can receive a NULL value from an external source and maintain the NULL value until you try to use it. ColdFusion will convert the NULL into an empty string (in the case of recordset) or potentially destroy the variable altogether. However now with greater support for "NULL" values, ColdFusion allows you to pass in and return a NULL value from a method. IsNull() instruction will test for NULL values and return true or false.

If you have three eggs, eat three eggs, then you might think you have nothing , but in terms of eggs you have "0". Zero is something, it's a number, and it's not nothing.

A large percentage of the errors you encounter while writing CFML code will involve a variable not existing. You thought something was there, you tried to do something to it, and you can't do something to nothing so CFML creates an error. Lets rewrite our makeEggs method to illustrate NULL :

Tag Syntax

<cffunction name = "makeEggs" returnType = "array">
    <cfargument name = "quantity" type="numeric">

    <cfif IsNull(arguments.quantity)>
        <cfset local.makeEggs = "How am I supposed to make nothingness number of eggs?" />
    <cfelse>
        <cfset local.makeEggs = "Making your #arguments.quantity# eggs!" />
        <cfset local.yourEggs = ArrayNew(1) />

        <cfloop condition = "#ArrayLen(local.yourEggs)# LT #arguments.quantity#">
            <cfset ArrayAppend(local.yourEggs, "Making an Egg.") />
        </cfloop>
    </cfif>

    <cfreturn local.yourEggs />
</cffunction>

Script Syntax

public array function makeEggs (numeric quantity) {
    if(IsNull(arguments.quantity)) {
        local.makeEggs = "How am I supposed to make nothingness number of eggs?";
    } else {
        local.makeEggs = "Making your #arguments.quantity# eggs!";
        local.yourEggs = ArrayNew(1);
        while (ArrayLen(local.yourEggs) < arguments.quantity)
        ArrayAppend (local.yourEggs, "Making an Egg.");
    }

    return local.yourEggs;
}

Reload the file, call frank.makeEggs(3) then try frank.makeEggs().


Previous Conditionals

Clone this wiki locally