Mildred Shimz
Mildred Shimz

Reputation: 617

list index out of bounds (0)

i posted a question on Fixing an error in a method declaration in a form in Delphi but after getting it fixed another error popped up upon compiling and its saying project project1.exe raised exception class EStringListError with message 'list index out of bounds(0)'.when i press continue its not working but when i press break its flashing on the code neraz:=true; this is my code below

Procedure Reload;
var
    i:integer;
begin
form1.ListBox1.Clear;
form1.ListBox2.Clear;
if neraz then
HD;
neraz:=true;//..................here
form1.Label3.Caption:='free: '+inttostr(vs*32)+' byte'+#10#13+'cluster size = 32 bytes';
  i:=TABLE[nk1].nach;
   KolP1:=0; KolP2:=0;
   while (FAT[i]<>1024)  do begin
      if TABLE[fat[i]].tip then begin
          form1.ListBox1.Items.Add('dir>'+TABLE[fat[i]].name);
          inc(kolP1);
      end
      else
          if TABLE[fat[i]].format='txt' then
                form1.ListBox1.Items.Add('f_text> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format)
          else
                form1.ListBox1.Items.Add('f_bin> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format);
      if (fat[i]<>0) then
      i:=fat[i];
   end;
   i:=TABLE[nk2].nach;
   while (FAT[i]<>1024)  do begin
      if TABLE[FAT[i]].tip then begin
          form1.ListBox2.Items.Add('dir>'+TABLE[fat[i]].name);
          inc(kolP2)
      end
      else
          if TABLE[fat[i]].format='txt' then
                form1.ListBox2.Items.Add('f_text> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format)
          else
                form1.ListBox2.Items.Add('f_bin> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format);
      if (fat[i]<>0) then
      i:=fat[i];
   end;
   vfail;
end;


procedure HD;
var
  i: integer;
begin
  for i := 0 to 49 do begin
    with form2.ListView1.Items[i] do begin
      SubItems[0] := TABLE[i].name;
      SubItems[1] := TABLE[i].format;
      if TABLE[i].tip then
        SubItems[2] := 'folder'
      else
        SubItems[2] := 'file';
      SubItems[3] := IntToStr(TABLE[i].nach);
      SubItems[4] := IntToStr(TABLE[i].razmer);
    end;
    form2.ListView2.Items[i].SubItems[0] := IntToStr(fat[i]);
  end;
end;

Upvotes: 2

Views: 13594

Answers (1)

David Heffernan
David Heffernan

Reputation: 613481

The exception class EStringListError raises the error List index out of bounds (0) when you try to access a member of a TStrings instance that is empty. The most likely candidate for that is the SubItems property of the list items.

You would appear to have fallen into quite a common trap. Although you have created columns for the list view, you also need to fill out the SubItems list for each list item. A simple solution is to modify HD like this:

with form2.ListView1.Items[i] do begin
  while SubItems.Count<5 do
    SubItems.Add('');
  SubItems[0] := ...

Although it may in fact be better to add the sub-items at the same time that you create the list items. But I'm not showing code for that since you didn't include the part of your program that populates the lists.

Upvotes: 5

Related Questions