views:

75

answers:

2

I get this error

Element INSTANCE is undefined in VARIABLES.

I do not see the reason for the error!

This is my factory

<cfcomponent output="true" displayname="ObjectFactory">

 <cffunction name="init" access="public" output="true" returntype="ObjectFactory">
  <cfset variables.instance = structNew() />
  <cfreturn this />
 </cffunction>

 <cffunction name="createObj" access="public" output="false" returntype="any">
  <cfargument name="objName" type="string" required="true" />
  <cfswitch expression="#arguments.objName#">
   <cfcase value="abstractCollection">
    <cfreturn createObject('component',"AbstractCollection").init() />
    <cfbreak />
   </cfcase>
   <cfcase value="assignmentCollection">
    <cfreturn createObject('component',"AssignmentCollection").init() />
    <cfbreak />
   </cfcase>
   <cfcase value="salesmanBean">
    <cfreturn createObject('component',"SalesmanBean").init(
     salesmanHasThisDecorations = this.getInstance("assignmentCollection")) />
    <cfbreak />
   </cfcase>
  </cfswitch>
 </cffunction>

 <cffunction name="getInstance" access="public" output="false" returntype="any">
  <cfargument name="objName" type="string" required="true" />
 <!--- Error occurs in the line below --->
  <cfif not structKeyExists(variables.instance, arguments.objName)>
   <cfset variables.instance[arguments.objName] = this.createObj(arguments.objName) />
  </cfif>
  <cfreturn variables.instance[arguments.objName] />
 </cffunction>
</cfcomponent>
+4  A: 

Make sure you call init() when you instantiate ObjectFactory:

<cfset objectFactory = CreateObject("component","ObjectFactory").init()>

FYI, init() and <cfcomponent> should have output='false'

FYI, you should call your own function without "this.", because if for some reason the function is later declared as private, it won't find it in 'this' scope.

Henry
A: 

Agree that you are likely not calling .init() so are not creating the variable before accessing it.

You also may want to initialize (create) the VARIABLES scoped variables outside of init(). The init() should be used more for passing in values to your internal CFC-scope (VARIABLES scope) than for creating variables within it.

<cfcomponent displayname="ObjectFactory">
<cfset variables.instance = structNew() />

 <cffunction name="init" access="public" returntype="ObjectFactory">
  <cfargument name="name" required="yes" type="string">
  <cfset variables.instance.name = arguments.name>
  <cfreturn this />
 </cffunction>

...
mujimu
i resolved it by doing this:<cfset setName(arguments.name) />
mrt181