Why is Count=nil?

I have this code:

procedure Settings.Init( webSettings : WebConfigSettings; resolveBracketParameters : Boolean );

  var
     Count : Integer;

  function Substitute( var label : String; isLoginWelcomeLabel : boolean := false ) : boolean;

     function GetObserverSingular : String;
        begin
        if ObserverLabel.ToLower.EndsWith( 's' ) 
           then result := ObserverLabel.Substring( 0, ObserverLabel.Length -1 )
           else result := ObserverLabel;
        end;

     begin
     if label = nil then exit false;

     var newlabel := label.Replace( webSettings.LabelTags.AtRiskTag,         AtRiskLabel )
                          .Replace( webSettings.LabelTags.BarrierTag,        BarrierLabel )
                          .Replace( webSettings.LabelTags.Category1Tag,      Category1Label )
                          .Replace( webSettings.LabelTags.Category2Tag,      Category2Label )
                          .Replace( webSettings.LabelTags.Category3Tag,      Category3Label )
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory1Tag, ... ) // not yet
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory2Tag, ... ) // not yet
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory3Tag, ... ) // not yet
                          .Replace( webSettings.LabelTags.ObservationTag,    ObservationLabel )
                          .Replace( webSettings.LabelTags.ObserverTag,       if isLoginWelcomeLabel then ObserverLabel else GetObserverSingular )
                          .Replace( webSettings.LabelTags.NoteTag,           NoteLabel )
                          .Replace( webSettings.LabelTags.PointTag,          PointLabel )
                          .Replace( webSettings.LabelTags.SafeTag,           SafeLabel )
                          .Replace( webSettings.LabelTags.SiteNameTag,       SiteName )
                          .Replace( webSettings.LabelTags.SubCategory1Tag,   SubCategory1Label )
                          .Replace( webSettings.LabelTags.SubCategory2Tag,   SubCategory2Label )
                          .Replace( webSettings.LabelTags.SubCategory3Tag,   SubCategory3Label )
                          .Replace( webSettings.LabelTags.UserNameTag,       UserNameLabel )
                          .Replace( webSettings.LabelTags.TemplateTag,       TemplateLabel );

     newlabel :=  newlabel.Replace( webSettings.LabelTags.AtRiskTag.ToLower,       AtRiskLabel.ToLower )
                          .Replace( webSettings.LabelTags.BarrierTag.ToLower,      BarrierLabel.ToLower )
                          .Replace( webSettings.LabelTags.Category1Tag.ToLower,    Category1Label.ToLower )
                          .Replace( webSettings.LabelTags.Category2Tag.ToLower,    Category2Label.ToLower )
                          .Replace( webSettings.LabelTags.Category3Tag.ToLower,    Category3Label.ToLower )
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory1Tag, ... ) // not yet
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory2Tag, ... ) // not yet
                          // .Replace( webSettings.LabelTags.CategoryAndSubCategory3Tag, ... ) // not yet
                          .Replace( webSettings.LabelTags.ObservationTag.ToLower,  ObservationLabel.ToLower )
                          .Replace( webSettings.LabelTags.ObserverTag.ToLower,     if isLoginWelcomeLabel then ObserverLabel.ToLower else GetObserverSingular.ToLower )
                          .Replace( webSettings.LabelTags.NoteTag.ToLower,         NoteLabel.ToLower )
                          .Replace( webSettings.LabelTags.PointTag.ToLower,        PointLabel.ToLower )
                          .Replace( webSettings.LabelTags.SafeTag.ToLower,         SafeLabel.ToLower )
                          .Replace( webSettings.LabelTags.SiteNameTag,             SiteName.ToLower )
                          .Replace( webSettings.LabelTags.SubCategory1Tag.ToLower, SubCategory1Label.ToLower )
                          .Replace( webSettings.LabelTags.SubCategory2Tag.ToLower, SubCategory2Label.ToLower )
                          .Replace( webSettings.LabelTags.SubCategory3Tag.ToLower, SubCategory3Label.ToLower )
                          .Replace( webSettings.LabelTags.UserNameTag.ToLower,     UserNameLabel.ToLower )
                          .Replace( webSettings.LabelTags.TemplateTag.ToLower,     TemplateLabel.ToLower );

     result := newlabel <> label;

     label := newlabel;

     // stop it if it looks like there is an infinite loop going on (one tag could potentially substitute another tag)
     inc( Count );
     if Count > 10 then exit false;
     end;

  procedure SubstituteTags;
     begin
     // mlt
     
     // I'm not sure if my code works right here or not.
     // the intent is that all the [key] type parameters get replaced.
     // but I'm not sure if it is somehow order dependent.  
     // it seemed to work, so maybe it is okay, but maybe I just have them in the right order
     // gotta move on

     Count := 0; while Substitute( var AtRiskLabel            ) do;
     Count := 0; while Substitute( var AtRiskNotesLabel       ) do;
     Count := 0; while Substitute( var BarrierLabel           ) do;
     Count := 0; while Substitute( var BarriersLabel          ) do;
     Count := 0; while Substitute( var CancelLabel            ) do;
     Count := 0; while Substitute( var Category1Label         ) do;
     Count := 0; while Substitute( var Category2Label         ) do;
     Count := 0; while Substitute( var Category3Label         ) do;
     Count := 0; while Substitute( var DeleteLabel            ) do;
     Count := 0; while Substitute( var JobSafetyAnalysisLabel ) do;
     Count := 0; while Substitute( var LoginWelcomeLabel      , true ) do;
     Count := 0; while Substitute( var NoteLabel              ) do;
     Count := 0; while Substitute( var ObservationLabel       ) do;
     Count := 0; while Substitute( var ObserverLabel          ) do;
     Count := 0; while Substitute( var PointCategoryLabel     ) do;
     Count := 0; while Substitute( var PointLabel             ) do;
     Count := 0; while Substitute( var SafeLabel              ) do;
     Count := 0; while Substitute( var SafeNotesLabel         ) do;
     Count := 0; while Substitute( var SaveLabel              ) do;
     Count := 0; while Substitute( var SubCategory1Label      ) do;
     Count := 0; while Substitute( var SubCategory2Label      ) do;
     Count := 0; while Substitute( var SubCategory3Label      ) do;
     Count := 0; while Substitute( var TemplateLabel          ) do;
     Count := 0; while Substitute( var ThoughtOfDayLabel      ) do;
     Count := 0; while Substitute( var UserNameLabel          ) do;
     end;

begin

SubstituteTags;
end;

And when it first goes into Substitute and gets down to the inc(Count), Count is NIL! The AtRiskLabel is a string. It WAS working until I added the GetObserverSingular local function.

p.s. How do I put in code in this editor so that it doesn’t lose the indentation?

I’m a little confused about what exactly changed that broke the code above, but when I moved the Count variable out of the local scope and put in the object class, the code worked again.

I really want a local Count.

Hello

And when it first goes into Substitute and gets down to the inc(Count), Count is NIL!

I tried to reproduce the situation at simple testcase and couldn’t. IT will be very helpful if you send us a simple testcase that shows a problem.
The code you posted here looks fine, the Count var is assigned so it shouldn’t be nil in any case.
The integer type (which is type alias for Int32) is not nullable type. It cannot be nil even it is not assigned.

How do I put in code in this editor so that it doesn't lose the indentation?

You can put your code into next tags:

pascal code here< /pre> 
Make sure you delete space in the "< /pre>" tag when inserting pascal code.

Hope that helps.

I’m not sure why it didn’t work, but that IS the code. I’ll try moving the Count back to being a local variable and see if the problem recurs. If it does, then I could try to strip out stuff to make a simpler case. Of course, since I don’t know what about it makes it fail, I’m not sure HOW to make a simpler case, but I’ll give it a whack.

Oh, and how were we supposed to know that would could use the “<pre>” tag?

Odd, I put the code back the way it was here, and now it is working. If I didn’t see it fail before, multiple times, I wouldn’t believe it. I guess I won’t try to make a simpler case since the original is working now (as it did originally).