Skip to main content
Inspiring
June 20, 2021
Answered

How to set null value into property

  • June 20, 2021
  • 2 replies
  • 3388 views

Hi,

 

I tried to set null to property and I am using

property name="Company" type="string" setter="true" getter="true" default="null";
 

and for some reason when I serialized it, the data is becoming string instead of null value although I checked the "Enable Null Support".

Any idea how to set this up or is this a bug?

 

Thanks. 

    This topic has been closed for replies.
    Correct answer Charlie Arehart

    @joseph.yan You're hitting a couple of different isues there.

     

    First, since you assigned the right-hand value as "null" (with quotes), that did indeed set your company property to the string value of "null". And normally, one might suggest that you should have instead set that right-hand value as just null (no quotes). And that would work about anywhere else in CFML.

     

    But here you'd hit a second problem: you'd get the errror, "Expression in cfproperty value must have a constant value." The issue is that the default keyword for the property of a CFC (or the property attribute of cfproperty) is designed to accept only a constant (like a string, number, etc). It can't be a variable or function.

     

    And we could argue that you SHOULD be able to set it to null (as that is indeed a constant), but apparently it's not. (You may want to open a bug report/feature request at tracker.adobe.com, which would only take a minute.)

     

    But all is not lost. First, you could work around this by setting the default to "", and then in whatever method you would use it (or an init function) you could test if the value is indeed "" (nothing overrode that default) and if so, set it to null, as in

    if (company is "") company=null;

    And you can confirm that that property is indeed null with the CFML function isnull().

     

    Let us know if that works for you. And if you file the bug report/feature request, please do report the number here.

    2 replies

    BKBK
    Community Expert
    Community Expert
    June 22, 2021
    quote

     

    ... is this a bug?


    By @joseph.yan

     

    No, it isn't. "null" is just a string, like any other.

     

     

    Any idea how to set this up ... ?

     

     

    Yes. The default value of a cfproperty is null. So, just omit the default attribute:

     

    <cfproperty name="Company" type="string" setter="true" getter="true">

     

     

    Test it yourself:

    1) Create the following CFC and CFM, and place them in the same directory under the web root.

    2) Run testIt.cfm.

     

    You will see that the values of the properties Company and SomeProperty are both null.

     

     

    <!--- Test.cfc --->
    <cfcomponent accessors="true">
    	<cfproperty name="Company" type="string" setter="true" getter="true">
    	<cfproperty name="SomeProperty" type="string" setter="true" getter="true">
    </cfcomponent>
    
    <!--- testIt.cfm --->
    <cfset testObject = createObject("component", "Test")>
    <cfdump var="#testObject#" >

     

     

    BKBK
    Community Expert
    Community Expert
    July 4, 2021

    @joseph.yan , did that help?

    Inspiring
    July 6, 2021

    Yes, I am following Charlie. Thank you so much for you guys help.

    John T Smith
    Community Expert
    Community Expert
    June 20, 2021

    Please post the exact name of the Adobe program you use so a Moderator may move this message to that forum

    Charlie Arehart
    Community Expert
    Charlie ArehartCommunity ExpertCorrect answer
    Community Expert
    June 20, 2021

    @joseph.yan You're hitting a couple of different isues there.

     

    First, since you assigned the right-hand value as "null" (with quotes), that did indeed set your company property to the string value of "null". And normally, one might suggest that you should have instead set that right-hand value as just null (no quotes). And that would work about anywhere else in CFML.

     

    But here you'd hit a second problem: you'd get the errror, "Expression in cfproperty value must have a constant value." The issue is that the default keyword for the property of a CFC (or the property attribute of cfproperty) is designed to accept only a constant (like a string, number, etc). It can't be a variable or function.

     

    And we could argue that you SHOULD be able to set it to null (as that is indeed a constant), but apparently it's not. (You may want to open a bug report/feature request at tracker.adobe.com, which would only take a minute.)

     

    But all is not lost. First, you could work around this by setting the default to "", and then in whatever method you would use it (or an init function) you could test if the value is indeed "" (nothing overrode that default) and if so, set it to null, as in

    if (company is "") company=null;

    And you can confirm that that property is indeed null with the CFML function isnull().

     

    Let us know if that works for you. And if you file the bug report/feature request, please do report the number here.

    /Charlie (troubleshooter, carehart. org)