Lazarus

Programming => General => Topic started by: mangakissa on October 21, 2020, 12:16:53 pm

Title: Generics is nog givving a 'nil' back as result
Post by: mangakissa 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.  
Title: Re: Generics is nog givving a 'nil' back as result
Post by: Blaazen 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
Title: Re: Generics is nog givving a 'nil' back as result
Post by: marcov on October 21, 2020, 01:02:06 pm
Hard to say without fheader() code and reproduction.
Title: Re: Generics is nog givving a 'nil' back as result
Post by: Blaazen 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.
Title: Re: Generics is nog givving a 'nil' back as result
Post by: mangakissa 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.
Title: Re: Generics is nog givving a 'nil' back as result
Post by: Blaazen 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.
TinyPortal © 2005-2018