PDA

View Full Version : Help with .cfc


maasss18
11-15-2008, 09:14 AM
I encounter this error when i run my application.
Unable to invoke CFC - You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

What does this error means? Is this the correct way on how to write a .cfc file?

This is my .cfc code:

<cffunction name="vCreate" access="public" output="false" returntype="ModelDb">
<cfargument name="aVarTest" type="ModelDb" required="true" />

<!--- var scope everything! --->
<cfset var insertVar = "" />
<cfset var checkObjectName = "" />
<cfset var getVarID = "" />

<cftry>
<cftransaction action="begin">

<cfquery name="checkObjectID" datasource="#variables.dsn#">
SELECT objectID as objID
FROM Objects
WHERE objectName = <cfqueryparam value="#aVarTest.getObjectName()#" cfsqltype="cf_sql_varchar" />
</cfquery>

<cfscript>
aVarTest.setObjectID(checkObjectName.objID) ;
</cfscript>

<!---throw error if records were found--->
<cfif checkObjectID.recordCount NEQ 0>
<cfquery name="insertVar" datasource="#variables.dsn#">
INSERT INTO Variables ( objectID, variableName, variableView, varInitialValue, varVSliderValue, varValueUnit, varMaxValue, variableHigh, variableMedium, variableLow, variableDesc, projectID )
VALUES (
//Is this the correct way to enter a value from the previous query?
<cfqueryparam cfsqltype="cf_sql_numeric" value="checkObjectName.objID" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableName()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableView()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVarInitialValue()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#aVarTest.getVarVSliderValue()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVarValueUnit()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#aVarTest.getVarMaxValue()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableHigh()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableMedium()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableLow()#" maxlength="40" null="no">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#aVarTest.getVariableDesc()#" null="no">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#aVarTest.getProjectID()#" null="no">)
</cfquery>


<cfquery name="getVarID" datasource="#variables.dsn#">
SELECT @@identity as newVarID
</cfquery>

<cfscript>
aVarTest.setVariableID(getVarID.newVarID) ;
</cfscript>
</cfif>

<cfif checkObjectID.recordCount EQ 0>
<cfthrow type="ModelDb.NoRecordsFound" message="No Objects" />
</cfif>

<!---if we have made it this far - all queries were successful so commit changes to database--->
<cftransaction action="commit" />
</cftransaction>

<cfcatch type="database"> <!---there was a problem with a query--->
<cftransaction action="rollback" />
<cfthrow type="ModelDb.vCreate" message="#cfcatch.detail#" />
</cfcatch>
</cftry>

<cfreturn aVarTest />
</cffunction>

Sly_cardinal
11-16-2008, 01:10 PM
The error means that you have not properly instantiated one of your objects correctly.

At the start of your function you correctly define your local variables (as Strings, analogous to null in Coldfusion):


<cfset var insertVar = "" />
<cfset var checkObjectName = "" />
<cfset var getVarID = "" />


But then you access checkObjectName.objID without instantiating your object:


<cfscript>
aVarTest.setObjectID(checkObjectName.objID) ;
</cfscript>


This means that you're trying to access the non-existant objID property of a String.

Sly_cardinal
11-16-2008, 01:12 PM
Also a quick note: You don't have to keep switching to cfscript mode just to call a single function.

e.g.

<cfscript>
aVarTest.setObjectID(checkObjectName.objID) ;
</cfscript>


You can do the same thing by using the cfset tag:


<cfset aVarTest.setObjectID(checkObjectName.objID) />


You don't actually have to assign the result to anything - the two examples above are exactly equivalent and you dont have to have all the extra <cfscript>...</cfscript> stuff just for one line of code :)

maasss18
11-18-2008, 11:58 AM
Thank you so so much!! It works!!