Steve
Steve

Reputation: 3095

Coldfusion 10 - Element [n] is undefined in a Java object of type class coldfusion.runtime.Array

I recently upgraded a system from CF8 to CF10 and have one bug that I'm having problems tracking down. It has to do with a remote API call that gets a JSON string back and that string then gets converted to a query object. That's where I'm coming across the error:

Element [n] is undefined in a Java object of type class coldfusion.runtime.Array. The problem is in the function that converts the string to a query.

<cffunction name="CFjsonToQuery" access="public" returntype="query" output="no">
  <cfargument name="cfData" required="yes" type="struct"/>
  <cfset var LOCAL = {}/>
    <cfset LOCAL.tmpQry = QueryNew( ArrayToList(ARGUMENTS.cfData.Data.COLUMNS) ) />
       <cfloop index = "i" from = "1" to = "#ArrayLen(ARGUMENTS.cfData.Data.DATA)#">
         <cfset LOCAL.Row = QueryAddRow(LOCAL.tmpQry) />
           <cfloop index="k" from="1" to="#ArrayLen(ARGUMENTS.cfData.Data.DATA[i])#">
             <cfset LOCAL.colName = ARGUMENTS.cfData.Data.COLUMNS[K]/>
             <cfset QuerySetCell(LOCAL.tmpQry,LOCAL.colName,ARGUMENTS.cfData.Data.DATA[i][k],LOCAL.Row)/>
           </cfloop>    
       </cfloop>
  <cfreturn LOCAL.tmpQry/>
</cffunction>    

Anywhere the JSON returns 'null' (i.e. "...","19107-3609",null,null,null,"...") the error is thrown. I've tried using isNull to check if it's null in the cfloop:

<cfif isNull(ARGUMENTS.cfData.Data.DATA[i][k])>
   <cfset ARGUMENTS.cfData.Data.DATA[i][k] = 'I AM NULL'/>
</cfif>

EDIT - here's a simplified example - the issue is the way the newer deserializeJson() works I believe:

<cfset jstr = '{"SUCCESS":true,"ERRORS":[],"DATA":{"COLUMNS":["ID","FNAME","LNAME"],"DATA":[[390132,"steve",null]]}}'/>
<cfset cfData = deserializeJson(jstr) />
  <cfloop index = "i" from = "1" to = "#ArrayLen(cfData.Data.DATA)#">
    <cfset Row = QueryAddRow(tmpQry) />
      <cfloop index="k" from="1" to="#ArrayLen(cfData.Data.DATA[i])#">
        <cfset colName = cfData.Data.COLUMNS[K]/>
        <cfset QuerySetCell(tmpQry,colName,cfData.Data.DATA[i][k],Row)/>
      </cfloop>
  </cfloop>

I've tried all sorts of tests for empty string, isNull etc. and I'm still not sure how to get the query object built if deserializejson returns:

[undefined array element] Element 3 is undefined in a Java object of type class coldfusion.runtime.Array.

This does seem to work:

<cfset cfData = deserializeJson(returnData,'FALSE') />
<cfset qryData = cfData.data />

This lets me then use qryData as if it were a normal cfquery.

Upvotes: 3

Views: 3808

Answers (2)

Scott Gearhart
Scott Gearhart

Reputation: 1161

You can do a check if the element is undefined using the CF Function ArrayIsDefined(array, elementIndex)

Upvotes: 2

Steve
Steve

Reputation: 3095

What I've done for now is add 'FALSE' to the deserializeJSON strictMapping flag and that seems to automatically create a query object? I'll admit though this is getting into the underpinnings of CF10 and I could be wrong on that. I'll update my code above for visual clarity.

Upvotes: 1

Related Questions