Skip to main content
Paolo Olocco
Participating Frequently
November 21, 2025
Answered

ColdFusion safe-navigation-operator used like boolean

  • November 21, 2025
  • 2 replies
  • 554 views

Hi,

i use this code in CF2023

<cfscript>
params = {
  field1 = true
};

foo(args=params);

void function foo(required struct args) {
  if (arguments.args?.field2) {
    writeOutput("entra");
  } else {
    writeOutput("non entra");
  }
}
</cfscript>

but i receive error 

Cannot invoke "Object.getClass()" because "x" is null

in first time, after only java.lang.NullPointerException

 

In CF2025 continue with previuos error

 

Am I doing something wrong or is this a bug?

    Correct answer BKBK

    I think ColdFusion is behaving as expected. As

    arguments.args.field2

    does not exist, the safe-navigation operator (?.) returns null as the value of arguments.args?.field2.
    Then the code 

    if (arguments.args?.field2) {
    
    }

    tells ColdFusion to cast a null to a boolean, hence the error.

     

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    November 21, 2025

    I think ColdFusion is behaving as expected. As

    arguments.args.field2

    does not exist, the safe-navigation operator (?.) returns null as the value of arguments.args?.field2.
    Then the code 

    if (arguments.args?.field2) {
    
    }

    tells ColdFusion to cast a null to a boolean, hence the error.

     

    Charlie Arehart
    Community Expert
    Community Expert
    November 21, 2025

    Paolo, some good news is that there is a very simply single tweak that will get your code working.

     

    First, though, I don't think you'll have found that code to have ever worked (as you expect) in any CF version, right? To be clear, the result of using the safe nav operator has never itself been a boolean. It's always been an "undefined" result (if the operator on the right doesn't exist). And it's because of this that your error occurs, because of course an IF test expects an expression which evaluates to boolean. 

     

    You can even prove this without all your code (there's nothing about your if being in a method that changes things). This first example shows how the dump of the result is indeed "undefined":

    <cfscript>
    params = {field1 = true};
    writedump(params?.field2);
    </cfscript>

    Then as for trying to use the result as a boolean, this next example shows (more simply) how doing that fails with the same error you show:

    <cfscript>
    params = {field1 = true};
    if (params?.field2) {
        writeOutput("entra");
        }
    </cfscript>

     

    Finally, if you're wanting your code to "run" even if that field2 is undefined, you could tweak it simply by adding the elvis operator to the end of the line, which tells CF that if the result of the expression is indeed undefined, then it should be set to a default provided on the right side of the elvis operator. Tweaking my example above, this outputs your string:

    <cfscript>
    params = {field1 = true};
    if (params?.field2?: true) {
        writeOutput("entra");
        }
    </cfscript>

    And implementing that in your code requires changing only that if line:

    <cfscript>
    params = {
      field1 = true
    };
    
    foo(args=params);
    
    void function foo(required struct args) {
      if (arguments.args?.field2?:true) {
        writeOutput("entra");
      } else {
        writeOutput("non entra");
      }
    }
    </cfscript>

    And it outputs the "entra" string. Of course, if you prefer it to return the "non entra" string, change the if line to end with false instead.

     

    Let us know what you think.

    /Charlie (troubleshooter, carehart. org)
    Paolo Olocco
    Participating Frequently
    November 24, 2025

    Hi Charlie, thanks for your clarification but your example has true, instead false

    So, correct is:

    <cfscript>
    params = {field1 = true};
    if (params?.field2 ?: false) {
      writeOutput("enter");
    } else {
      writeOutput("don't enter");
    }
    </cfscript>

    Furthermore, if the variable (used for form fields) also has an empty string value, you should use a solution like this:

    if ((params?.field2==true) ?: false) {
      writeOutput("enter");
    } else {
      writeOutput("don't enter");
    }

    I'm a bit concerned about using safe-navigation together with the ternary/elvis operator as it affects the reading/understanding of the code in my opinion.

    What do you think?

    BKBK
    Community Expert
    Community Expert
    November 24, 2025
    quoteI'm a bit concerned about using safe-navigation together with the ternary/elvis operator as it affects the reading/understanding of the code in my opinion.

    What do you think?


    By @Paolo Olocco

    I agree with you, @Paolo Olocco .

     

    The safe-navigation and elvis operators each condenses multiple functionalities together, including the underlying assumptions. That is, they increase the code's complexity. In so doing, they make the code more difficult to debug and to maintain than if you had used the operator-less version of the code.