Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFLoop in MX7 throws up Null Pointer Intermitently

Status
Not open for further replies.

MinalMoe

IS-IT--Management
Mar 25, 2004
62
GB
Using CFLoop with the following code:-

<cfloop index="i" from="1" to="#Ch_Pass_No#">
<cfset Ch_Prod = DateDiff("yyyy", #Form["Ch_DOB" & i]#, LDay)>
<cfset Age_Array[j] = "#DateFormat(Form["Ch_DOB" & i], "dd-mmm-yyyy")#">
<cfset j = j + 1>
<cfif Ch_Prod GT 12>
<cfset #Ad_Num# = #Ad_Num# + 1>
<cfset #Ch_Num# = #Ch_Num# - 1>
<cfset Ad_Change = 1>
<cfset Ch_Change = 1>
<cfelse>
<cfset Transfer_Age_Array[k] = "#Ch_Prod#">
<cfset k = k + 1>
</cfif>
</cfloop>

It has been working fine for a long while - now every so often its starting to throw up a an undefined value (Null Pointer) for the variable #Form["Ch_DOB" & i]#.

Is there anything I can do to improve the code?

Thanks
 
What type of form field is Form["Ch_DOB" & i]?

If there is a possibility the field may not exist or may have an invalid value, I would wrap the inner code section in a cfif statement. The cfif should use IsDefined() to ensure the variable exists and IsDate() to ensure the value is a validate date before proceeding.
 
try this:

Code:
<cfloop index="i" from="1" to="#Ch_Pass_No#">
 <cfif IsDefined("form.Ch_DOB#i#") AND Len(Trim(form["Ch_DOB#i#"]))>
   <cfset Ch_Prod = DateDiff("yyyy", #Form["Ch_DOB" & i]#, LDay)>
   <cfset Age_Array[j] = "#DateFormat(Form["Ch_DOB" & i], "dd-mmm-yyyy")#">
   <cfset j = j + 1>
   <cfif Ch_Prod GT 12>
      <cfset #Ad_Num# = #Ad_Num# + 1>
      <cfset #Ch_Num# = #Ch_Num# - 1>
      <cfset Ad_Change = 1>
      <cfset Ch_Change = 1>
   <cfelse>
   <cfset Transfer_Age_Array[k] = "#Ch_Prod#">
   <cfset k = k + 1>      
   </cfif>
 </cfif>
</cfloop>

We never fail, we just find that the path to succes is never quite what we thought...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top