6
Var
  i : Integer;
  j : Integer;
  oSLArray : array of TStringList;
  oSL : TStringList;
begin
  SetLength(oSLArray, emailPassword.Lines.Count);
  for i := 0 to emailPassword.Lines.Count - 1 do
    {oSLArray[i] := TStringList.Create;
    oSLArray[i].Delimiter := ' ';
    oSLArray[i].DelimitedText := emailPassword.Lines[i];
    for j := 0 to oSLArray[i].Count-1 do begin
      Showmessage( oSLArray[i].Strings[j] );
    end; }
    oSL := TStringList.Create;
    oSL.Delimiter := ' ';
    oSL.DelimitedText := emailPassword.Lines[i];
    for j := 0 to oSL.Count-1 do begin
      Showmessage( oSL[j] );
    end;
  end;

I'm trying to make an array of TStringLists, read what's coming from the RichEdit 'EmailPassword', and then print it (I will put it in an array when I get that far).

When I uncomment out oSLarray, I get an access violation. When I tried it with oSL, nothing printed.

Now, I understand an access violation means the pointer may not be set properly, as I think the access violation is occurring at oSLArray[i] := TStringList.Create.

Am I just missing something small?

2
  • is emailPassword nil? That would cause an AV, but a few li9nes earlier. Commented Oct 13, 2011 at 14:08
  • What do you want us to do with the code that is commented out? Commented Oct 13, 2011 at 14:13

2 Answers 2

8

I've corrected the code, I believe this code will work, but I've only tested it in my head.

var 
  i : Integer; 
  j : Integer; 
  oSLArray : array of TStringList; 
  oSL : TStringList; 
begin
  if not(Assigned(emailpassword)) then exit;
  SetLength(oSLArray, emailPassword.Lines.Count); 
  for i := 0 to emailPassword.Lines.Count - 1 do begin
    oSLArray[i] := TStringList.Create; 
    oSLArray[i].Delimiter := ' '; 
    oSLArray[i].DelimitedText := emailPassword.Lines[i]; 
    for j := 0 to oSLArray[i].Count-1 do begin 
      Showmessage( oSLArray[i].Strings[j] );   <<--- The error has here
    end; {for j} 
  end; {for i}

    //oSL := TStringList.Create; 
    //try
    //  oSL.Delimiter := ' '; 
    //  oSL.DelimitedText := emailPassword.Lines[i]; 
    //  for j := 0 to oSL.Count-1 do begin 
    //    Showmessage( oSL[j] ); 
    //  end; {for j}
    //finally
    //  oSL.Free;
    //end; {try}
    //end; {for i} 
end;

Here's your old code with comments:

for i := 0 to emailPassword.Lines.Count - 1 do //don't forget begin
  oSLArray[i] := TStringList.Create; 
  oSLArray[i].Delimiter := ' '; 
  oSLArray[i].DelimitedText := emailPassword.Lines[i]; 
//<<<--  Here for i loop should end, but it does not.
    for j := 0 to oSLArray[i].Count-1 do begin 
 //You loop though all members of OSLArtray, even though only the first item is set, 
 //the rest is unassigned.
      Showmessage( oSLArray[i].Strings[j] );  <<-- Access Violation 
    end; } 
Sign up to request clarification or add additional context in comments.

5 Comments

Ah! The missing begin! Since I never write code without begin I've lost the ability to spot that it might be missing.
@DavidHeffernan, not often that one gets a chance to out-debug you.
Oh I don't know about that. Happens all the time. Anyway, you trounced good and proper on this one! +1
You are using i outside the for loop! The j-loop should be inside the i-loop.
Once more +1 for the school of NEVER EVER leave out the Begin / End.
5

The absence of a begin/end pair is the problem. Without the comments, the

for i := 0 to emailPassword.Lines.Count - 1 do

loop iterates only the line

oSLArray[i] := TStringList.Create;

the line

oSLArray[i].Delimiter := ' '; 

is executed after the loop.

1 Comment

+1 for providing the explanation as to why the OP's code didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.