I have a procudure that runs fine and outputs accurate information. It even executes the final command where I ask it to display "Done!" and displays the message box with "Done!" on it. After that, the darn thing gives me an Access violation error message. Been wrestling with it all day, but I can't figure out what is going on. Would appreciate any insights.

Thanks, and here is the procedure:

procedure THillSlopeRegr.ComputeCmdBtnClick(Sender: TObject);

var

I, K, NumValues: integer;

EstimatedValues: string;

EC50, HillSlope, SumSqDevX, SumSqDevY, SumCrossProds, SumSqDev_YX, MnSumSqDev_YX: double;

InputFile: TStringList;

X_Array, Y_Array, YHat_Array, SqDevX_Array, SqDevY_Array, CrossProds_Array, SD_YX_Array, CILL_Array, CIUL_Array, SENewY_Array, PILL_Array, PIUL_Array: TVal_Array;

MyFile: TextFile;

const Letters = ['A'..'Z', 'a'..'z'];

begin

InputFile := TStringList.Create;

try

InputFile.LoadFromFile(OpenFileEditBx.Text);

if YesHeadersRadBtn.Checked = True then K := 1

else if NoHeadersRadBtn.Checked = True then K := 0

else ShowMessage('Does your file have headers?' + #13#10 + 'Please answer Yes or No.');

SetLength(X_Array, InputFile.Count - K);

SetLength(Y_Array, InputFile.Count - K);

//Get X and Y values

X_Array := GetXValues(K, InputFile);

Y_Array := GetYValues(K, InputFile);

NumValues := Length(X_Array);

//Obtain the estimated values of EC50 and Hillslope as a single comma-delimited string

try

EstimatedValues := Estimate(NumValues, X_Array, Y_Array);

except

if (EstimatedValues = '') then MessageDlg('unable to compute EC50 and HillSlope parameters!', mtError, [mbOK], 0);

end;

ShowMessage(EstimatedValues);

//get the two parameters from the comma delimited string result of the Estimate function

EC50 := GetFirstValue(EstimatedValues);

HillSlope := GetSecondValue(EstimatedValues);

//Obtain y-hat values

YHat_Array := Regress(NumValues, K, X_Array, EC50, HillSlope);

//The following computations are for the CI and PI - based on Snedecor and Cochran (S&C) pp 160-167

//compute array of SumSq dev from mean for X and Y

SqDevX_Array := GetSqDevVals(NumValues, X_Array);

SumSqDevX := Sum(SqDevX_Array);

SqDevY_Array := GetSqDevVals(NumValues, Y_Array);

SumSqDevY := Sum(SqDevY_Array);

//Compute array of sum of products of

CrossProds_Array := GetCrossProds(NumValues, X_Array, Y_Array);

SumCrossProds := Sum(CrossProds_Array);

//Compute SumSqDev_YX or Sum(d2_YX) in S&C

SumSqDev_YX := SumSqDevY - Power(SumCrossProds, 2)/SumSqDevX;

//Compute Mean SumSqDev_YX (s2_YX in S&C)

MnSumSqDev_YX := SumSqDev_YX/(NumValues - 2);

//Compute ResMnSq of predicted population regr line

SD_YX_Array := GetSD_YX(NumValues, MnSumSqDev_YX, SumSqDevX, SqDevX_Array);

//Get Confidence Limits

CILL_Array := GetConfLL(NumValues, YHat_Array, SD_YX_Array);

CIUL_Array := GetConfUL(NumValues, YHat_Array, SD_YX_Array);

//Compute SE of new Y value

SENewY_Array := GetSENewY(NumValues, MnSumSqDev_YX, SumSqDevX, SqDevX_Array);

//Get Prediction Limits

PILL_Array := GetPredLL(NumValues, YHat_Array, SENewY_Array);

PIUL_Array := GetPredUL(NumValues, YHat_Array, SENewY_Array);

AssignFile(MyFile, 'C:\Users\...\Results.csv');

ReWrite(MyFile);

WriteLn(MyFile, 'X ' + ', ' + 'Y ' + ', ' + 'Y_Hat ' + ', ' + 'CILL ' + ', ' + 'CIUL ' + ', ' + 'PILL ' + ', ' + 'PIUL');

for I := 0 to NumValues - 1 do

begin

WriteLn(MyFile, FloatToStr(X_Array[I]) + ', ' + FloatToStr(Y_Array[I]) + ', ' +

FloatToStr(YHat_Array[I]) + ', ' + FloatToStr(CILL_Array[I]) + ', ' +

FloatToStr(CIUL_Array[I]) + ', ' + FloatToStr(PILL_Array[I]) + ', ' +

FloatToStr(PIUL_Array[I]));

end;

finally

CloseFile(MyFile);

InputFile.Free;

X_Array := nil;

Y_Array := nil;

YHat_Array := nil;

SqDevX_Array := nil;

SqDevY_Array := nil;

CrossProds_Array := nil;

SD_YX_Array := nil;

CILL_Array := nil;

CIUL_Array := nil;

SENewY_Array := nil;

PILL_Array := nil;

PIUL_Array := nil;

ShowMessage('Done!');

end;

end;

Tweet This+ 1 thisPost To Linkedin