我有一个函数,它返回一个从静态文本构建的字符串值并传递参数:
<cffunction name="additionalText" access="private" returntype="String"
hint="Adds additional text around the field value for certain dataTypes.">
<cfargument name="type" type="String" required="true" />
<cfargument name="val" type="String" required="true" />
<cfscript>
var text = ARGUMENTS.val;
if (ARGUMENTS.type == "A")
text = ARGUMENTS.val & "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";
else if (ARGUMENTS.type == "B")
text = ARGUMENTS.val & "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum molestie pretium sodales. Aliquam neque eros, aliquet nec tincidunt rutrum, commodo sit amet enim. Pellentesque habitant";
// Bad one - causing errors
else if (ARGUMENTS.type == "C")
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum molestie pretium sodales. Aliquam neque eros, aliquet nec tincidunt rutrum, commodo sit amet enim. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse vitae orci sed arcu semper convallis ut nec quam. Fusce viverra, turpis vestibulum congue tristique, arcu dolor feugiat orci, non vestibulum orci nisi non lorem. Donec et urna ligula. Sed vulputate lacinia orci, in consequat mauris tincidunt vel. Nam molestie rhoncus arcu, eget vestibulum neque commodo consequat. Nullam scelerisque condimentum nisi, et elementum quam sed. ";
return text;
</cfscript>
</cffunction>
“bad”字符串的长度为647.对函数的任何调用都会抛出ArrayIndexOutOfBounds异常。
当我缩短有问题的字符串时,不会发生异常。我这里没有选项 - 我需要完整的字符串才能使用。
为什么会发生这种情况,更重要的是,我该怎么做才能解决这个问题?
更新:
我只是试图从不同的位置调用该函数,它工作正常:
<cfscript>
function getIt(type, val){
return additionalText(type, val);
}
x.func = getIt;
</cfscript>
<cfdump var="#x.func("A", "XXXX")#" />
现在感到困惑......
更新:
以下是打开Robust Exception的ColdFusion异常的全文:
The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.
The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
Resources:
Check the ColdFusion documentation to verify that you are using the correct syntax.
Search the Knowledge Base to find a solution to your problem.
Browser Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Remote Address ....
Referrer ....
Date/Time 07-Sep-11 12:43 PM
Stack Trace (click to expand)
java.lang.ArrayIndexOutOfBoundsException
更新:
这是我如何调用函数....我有一个“字段”数组。数组中的每个“字段”对象都有几个属性,这些属性在别处设置。其中一个属性是“formatFunction”。某些字段具有formatFunction值,如“formatPhone”或“formatDate”,它们是字段对象中函数的名称:
Field.cfc:
<cfcomponent>
<cffunction name="init" access="package" returntype="Field" output="false">
<!--- Default format Function --->
<cfset THIS.formatFunction = additionalText />
<cfif THIS.name EQ "Date">
<cfset THIS.formatFunction = formatDate />
</cfif>
</cffunction>
<cffunction name="formatDate" access="private" returntype="String">
<cfargument name="val" type="String" required="true" />
<cfreturn DateFormat(ARGUMENTS.val, "dddd mmmm d, yyyy") />
</cffunction>
<cffunction name="additionalText" access="private" returntype="String"
hint="Adds additional text around the field value for certain dataTypes.">
<cfargument name="type" type="String" required="true" />
<cfargument name="val" type="String" required="true" />
<cfscript>
var text = ARGUMENTS.val;
if (ARGUMENTS.type == "A")
text = ARGUMENTS.val & "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";
else if (ARGUMENTS.type == "B")
text = ARGUMENTS.val & "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum molestie pretium sodales. Aliquam neque eros, aliquet nec tincidunt rutrum, commodo sit amet enim. Pellentesque habitant";
// Bad one - causing errors
else if (ARGUMENTS.type == "C")
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum molestie pretium sodales. Aliquam neque eros, aliquet nec tincidunt rutrum, commodo sit amet enim. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse vitae orci sed arcu semper convallis ut nec quam. Fusce viverra, turpis vestibulum congue tristique, arcu dolor feugiat orci, non vestibulum orci nisi non lorem. Donec et urna ligula. Sed vulputate lacinia orci, in consequat mauris tincidunt vel. Nam molestie rhoncus arcu, eget vestibulum neque commodo consequat. Nullam scelerisque condimentum nisi, et elementum quam sed. ";
return text;
</cfscript>
</cffunction>
</cfcomponent>
我在某些点循环遍历数组,为每个“字段”调用formatFunction:
for (i=1; i<=ArrayLen(THIS.aPDFFormFields); i=i+1) {
field = THIS.aPDFFormFields[i];
// get the value input in the FORM for the specified field
val = Trim(FORM[field.formFieldName]);
if (IsCustomFunction(field.formatFunction))
val = field.formatFunction(field.type, val);
}
这适用于所有其他字段(格式化电话号码,日期,时间等)。只有在包含这一条件时才会出错。
更新:
这个异常实际上是从另一个循环抛出的,我试图用值填充PDF表单:
<cfpdfform
source="myPDF.pdf"
action="populate"
destination="newPDF.pdf"
overwrite="yes"
overwritedata="yes">
<cfloop array="#THIS.aPDFFormFields#" index="field">
<cfpdfformparam
name="#field.name#"
value="#getFormattedFieldValue(FORM, field)#" />
</cfloop>
</cfpdfform>
<cffunction name="getFormattedFieldValue" returntype="string" output="false">
<cfargument name="fieldValues" type="Struct" required="true" />
<cfargument name="field" type="Field" required="true" />
<cfscript>
var fieldValue = "";
if (StructKeyExists(ARGUMENTS.fieldValues, fieldname))
fieldValue = ARGUMENTS.fieldValues[fieldName];
if (IsCustomFunction(field.formatFunction))
fieldValue = field.formatFunction(fieldValue);
if (!Len(fieldValue))
fieldValue = " ";
return fieldValue;
</cfscript>
</cffunction>
无法看到数组上的CFLOOP如何抛出ArrayOutofBounds异常。我没有在这个逻辑中的任何地方引用索引!
答案 0 :(得分:1)
当您尝试引用位于已定义数组边界之外的数组元素时,会发生 ArrayIndexOutOfBounds 错误。例如,你有一个包含5个元素的数组,并且你要求数组元素7,你就会得到错误。
你的函数返回一个字符串,我不知道它是如何导致错误的。无论调用该函数是什么,最有可能是错误的原因。
答案 1 :(得分:0)
你需要开始单独测试事物以弄清楚发生了什么。我使用Field.cfc运行了以下测试,但没有出现任何错误:
<cfset FORM.myFormField = RepeatString("*", 700) />
<cfset BaseObject = createObject("component", "Field") />
<cfset BaseObject.Name = "foo" />
<cfset BaseObject.Type = "C" />
<cfset BaseObject.formFieldName = "myFormField" />
<cfset BaseObject.init() />
<cfset aPDFFormFields = [] />
<cfloop from="1" to="500" index="i">
<cfset ArrayAppend(aPDFFormFields, Duplicate(BaseObject)) />
</cfloop>
<cfscript>
for (i=1; i<=ArrayLen(aPDFFormFields); i=i+1) {
field = aPDFFormFields[i];
// get the value input in the FORM for the specified field
val = Trim(FORM[field.formFieldName]);
if (IsCustomFunction(field.formatFunction)){
val = field.formatFunction(field.type, val);
writeOutput(i & ': ' & val & '<br />');
}
}
</cfscript>
正如Jason在上面的评论中所建议的那样,在那里放置一个try / catch,并找出数组中的哪个对象导致错误,并查看它是否可能缺少一些必要的信息以使事情正常工作。把事情分解成易消化的部分,你就会把错误搞得一团糟。
答案 2 :(得分:0)
事实证明,这个例外的原因虽然含糊不清,但是我试图用这个长字符串设置一个PDF表单复选框字段,它无法接受该值。我已要求PDF管理员相应地更新PDF,以便我们可以根据需要正确更新。