Recent

Author Topic: Generics is nog givving a 'nil' back as result  (Read 304 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1101
Generics is nog givving a 'nil' back as result
« on: October 21, 2020, 12:16:53 pm »
I trying to convert a file from my unix system to readable txt file.
My first record has fieldnames in it. The file want to convert has 83 fields.
Occasionally a 'record' line has 84 fields. I don't know. There's nothing special on it.
Before I save the found object to objectlist I get the right fieldname and fieldtype with the indexnumber. Because fheader locates 83 objects and not 84 it should headerfield result in nil.
But an error occurs before I can check if headerfield has values.
It shouldn't happen. the problem is also in Delphi.

Why is generics.collections not givving a nil back. Do I have to do another way to check?

Using Lazarus 2.0.10 / FPC 3.2 / Windows 10   

Code: Pascal  [Select][+][-]
  1. procedure Tconversion.SetRecords(aRegel: TStringlist; aMyRecord: TRecord; const aCharcase : smallint);
  2. var recordfield : TMyField;
  3.     headerfield : TMyField;
  4.     index       : integer;
  5. begin
  6.   for index := 0 to aRegel.Count - 1  do
  7.   begin
  8.     recordfield := TMyField.Create;
  9.     headerfield := fHeader[index];
  10.     if headerfield <> nil then
  11.     begin
  12.       recordfield.name  := headerfield.value;
  13.       recordfield.mydatatype := headerfield.mydatatype;
  14.     end else
  15.     begin
  16.       recordfield.name  := format('field%d',[index + 1]);
  17.       recordfield.mydatatype := 'character';
  18.     end;
  19.     recordfield.value := trim(aRegel[index]);
  20.     aMyRecord.fMyFields.Add(recordfield);
  21.   end;
  22. end;                          
  23.  
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Generics is nog givving a 'nil' back as result
« Reply #1 on: October 21, 2020, 01:01:54 pm »
I would test the Count:
Code: Pascal  [Select][+][-]
  1. if index<fHeader.Count then
  2.   begin
  3.     headerfield := fHeader[index];
  4.     {if headerfield <> nil then} //probably not necessary
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8901
  • FPC developer.
Re: Generics is nog givving a 'nil' back as result
« Reply #2 on: October 21, 2020, 01:02:06 pm »
Hard to say without fheader() code and reproduction.

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Generics is nog givving a 'nil' back as result
« Reply #3 on: October 21, 2020, 01:06:43 pm »
or you can loop like this:
Code: Pascal  [Select][+][-]
  1.   for index := 0 to Math.min(fHeader.Count ,aRegel.Count) - 1  do
  2.   begin
and manage the dangling item(s) separately after the loop.
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mangakissa

  • Hero Member
  • *****
  • Posts: 1101
Re: Generics is nog givving a 'nil' back as result
« Reply #4 on: October 21, 2020, 01:25:36 pm »
eventually I did. But why objectlist not givving a nil back inetead of an error. That should be happen (as my opinion).

After all I found the bug why one recordline has 84 columns insted of 83. TStringlist creates an extra entry when using this in the line: "hello world". I moved this in my unix database and all my records are 83 lines.
Tstringlist.Quotedchar doesn't help. Even strictdelimiter.

This is a general bug. Not only with FPC, but also in Delphi.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 2994
  • POKE 54296,15
    • Eye-Candy Controls
Re: Generics is nog givving a 'nil' back as result
« Reply #5 on: October 21, 2020, 02:07:17 pm »
I assume you use some generic list. If your list has 83 items and you try to read item no. 84 you'll get EListError - index out of bounds. It is the same for non-generic lists. After all, generic lists are internally non-generic. It's not a bug.
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018