ColdFusion Class definition error

Go To StackoverFlow.com

1

I have the following Applicaton.cfc

<cffunction name="onApplicationStart" access="public" returntype="Object">
 <cfset application.dsn = "myDB" />
 <cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />
 <cfreturn this />
</cffunction>

This is my component UserGateway.cfc

<cfcomponent name="UserGateway" hint="Data Access Object" output="false">
 <cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">
  <cfargument name="dsn" type="string" required="true" hint="datasource" />
   <cfset variables.dsn = arguments.dsn />
 <cfreturn this />
 </cffunction>

 <cffunction name="getUsers" access="public" output="false" returntype="query">
  <cfargument name="id" type="String" default="" />
  <cfargument name="name" type="String" default="" />
  <cfargument name="district" type="String" default="" />
  <cfset var qQuery = "" />
  <cfquery name="qQuery" datasource="#variables.dsn#">
    SELECT *
    FROM A INNER JOIN B
    ON A.X = B.Y
    WHERE 0=0
    <cfif "#arguments.id#" neq "">
     AND B.X LIKE '%#arguments.id#%'
    </cfif>
    <cfif "#arguments.name#" neq "">
     AND (A.I LIKE '#arguments.name#%'
      OR A.J LIKE '#arguments.name#%')
    </cfif>
    <cfif "#arguments.district#" neq "">
     AND A.O LIKE '%#arguments.district#%'
    </cfif>
  </cfquery>
  <cfreturn qQuery />
 </cffunction>
</cfcomponent>

And this is my same.cfm

<cfform action="same.cfm" method="post" preservedata="true">
 <p>ID: <cfinput type="text" name="id" size="20" maxlength="4" /></p>
 <p>Name: <cfinput type="text" name="name" size="20" maxlength="64" /></p>
 <p>District: <cfinput type="text" name="district" size="20" maxlength="3" /></p>
 <p><cfinput class="button" type="submit" name="submit" value="OK" /></p>
</cfform>

<cfif IsDefined("form.submit")>
 <table>
  <cfset qQuery = application.userGateway.getUsers(id = form.id, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>
 </table>
</cfif>

I get the following error:

Element USERGATEWAY is undefined in a Java object of type class [Ljava.lang.String;.
The error occurred in same.cfm: line 10

What am i missing?

-------------------------------------------
-------------------------------------------

When i do it this way it works. it must be something trivial that i as a beginner do not get.

Application.cfc

<cffunction name="onRequestStart" access="public" returntype="String">
 <cfset request.dsn="myDB" />
</cffunction>

same.cfm

    <cfset userGateway = createObject("component","cfc.UserGateway").init(dsn = request.dsn) />
    <cfset qGetUser = userGateway.getUsers(id = form.personid, name = form.name, district = form.district) />
  <cfoutput query="qQuery">
   <tr>
    <td>#qQuery.currentRow#.</a></td>
    <td>#qQuery.I#</a></td>
    <td>#qQuery.M#, #qQuery.N#</a></td>
    <td>#qQuery.D#</a></td>
   </tr>
  </cfoutput>
2009-06-16 10:16
by mrt181
I assume you have an Application.cfc in your project, and not Applicaton.cfc as written above - Vincent Buck 2009-06-16 12:23
yes, a typo in stackoverflo - mrt181 2009-06-16 15:05
most likely the problem is in your application.cfc. Could you post the entire application.cfc? If you have not named the application with This.name = "SomeName", then the variable saved in the application scope could be going somewhere else, thus the problem when you try to reference the usergateway - Jayson 2009-06-16 19:03


2

There are two things I see wrong here:

First, To my understanding, using the 'this' scope in application.cfc doesn't work the way you're trying to do it. By setting your userGateway object to an application scoped value, it becomes globally available and really makes returning it in onApplicationStart unnecessary. In your application.cfc, change your returntype to boolean and just return true; that should fix your problem.

Second, if in your query, your arguments and conditionals are not proxies of what you actually have, you're referencing an argument 'personid' which does not exist in your function. When calling that query through an object call in the application scope, I've seen the java string error returned as an error before as opposed to the CF Friendly 'variable doesn't exist' error.

2009-06-18 04:42
by K_Cruz
personid is a typo, should be of courese only i - mrt181 2009-06-19 13:41


0

In same.cfm, run this:

<cfset OnApplicationStart()>

Then refresh the page again. Does it now work?

2009-06-16 12:09
by Adrian Lynch
no, it does not hel - mrt181 2009-06-16 15:31


0

<cffunction name="init" access="public" hint="constructor" output="false" returntype="UserGateway">

should be:

<cffunction name="init" access="public" hint="constructor" output="false" returntype="Any">
2009-06-16 12:14
by rip747
sorry, does not help. i get the same erro - mrt181 2009-06-16 15:13


0

The following line is incorrect:

<cfset application.userGateway = createObject("component","cfc.UserGateway").init(dsn = application.dsn) />

It should read with out "cfc." at the beginning of the component name you want:

<cfset application.userGateway = createObject("component","UserGateway").init(dsn = application.dsn) />

Also, double check the rest of the application.cfc for correctness because something isn't running right, as you should have seen this error that it couldn't find component cfc.UserGateway.

EDIT: I also forgot to mention that onApplicationStart does not need to return anything. The return type should be void and no <return this/> needs to be present.

2009-06-16 13:52
by Jayson
sorry, does not help. i get the same erro - mrt181 2009-06-16 15:14
if the Usergateway was in a directory called cfc, this is correct, however, if all files are in the same directory, this is not correct. Please post your application.cfc, because the problem seems to be there - Jayson 2009-06-16 19:01


0



0

restart your CF service might help.

2009-06-16 18:42
by Henry
Ads