Question

I'm using <cfproperty /> to make use of implicit getters and setters in ColdFusion (Railo).

However, for more complex values like structs and arrays, how can I append to these?

<cfproperty name="settings" type="struct" />

How can I append an item into the property called settings? If I do the following:

<cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) />

I get the following error:

java.lang.NullPointerException

Am I missing something here? I'm new to the cfproperty tag and thought it would be a time saver, but I can't figure this out.

Also, as a bonus how would I set a default value to these complex data types?

Thanks, Mikey

Was it helpful?

Solution

Couple things here...

<cfset setSettings(structAppend(getSettings(), { "hello" = "world" })) />

Settings is a struct but structAppend() returns a boolean. Do your struct appending before this line. Second, structs are always passed by reference, meaning, if you do getSettings() you get a struct, which you can make changes to. Another call to getSettings() will return the same struct with the updated settings.

All you need is this:

<cfset structAppend(getSettings(), { "hello" = "world" }) />

One last thing. You could be getting a null pointer exception because getSettings() starts uninitialized. In your cfc, in the constructor area (after your properties), you should set an initial settings struct, like this:

<cfset setSettings({}) />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top