错误,因为ArrayLen的参数可能为空

时间:2012-05-30 23:04:44

标签: sql web-applications coldfusion

使用null为元素调用ArrayLen时,会抛出错误。你怎么让它返回0?

The value '' cannot be converted to a number.

The error occurred in D:/Resource/WWW/DEV/ww1dev.cwtvacations.com/welcome_survey/welcome_survey_question_builder.cfm: line 12

10 :    <cfset curPage = SESSION.thisPage />
11 : 
12 :    <cfloop from="1" to="#ArrayLen(SESSION.WHSurveyStruct.pagesQuestions[curPage])#" index="curQuestion">
13 :        <cfif (SESSION.WHSurveyStruct.pagesQuestions[curPage][curQuestion].SectionID eq 4 AND SESSION.WHSurveyStruct.AirlineID neq 0)   <!--- Ask airline questions, if airline known  --->
14 :                OR (SESSION.WHSurveyStruct.pagesQuestions[curPage][curQuestion].SectionID neq 4 

1 个答案:

答案 0 :(得分:0)

你可以做几件事。您可以使用三元运算符并将整个事物分配给变量:

upperBound = isNull(SESSION.WHSurveyStruct.pagesQuestions[curPage]) ? 0 : ArrayLen(SESSION.WHSurveyStruct.pagesQuestions[curPage]);

或者你可以事先检查一下。但是,您将数组的特定元素指定为数组似乎很奇怪。即SESSION.WHSurveyStruct.pagesQuestions[curPage]确实pagesQuestions[]包含数组还是数组?如果是数组,请删除[curParge]索引。