[SOLVED] NIL Pointer check Method lazarus / 62-General ---------------------------------------------------------------------- nasprin · 2021-11-09 23:29 Greetings! First post here, I searched, I failed, I started a topic someone somewhere, I'm sure, already answered  a 1000 times  I'm just starting the FreePascal/Lazarus Journey for my study and seem to have a NIL Pointer problem I'm stuck at, would appreciate a hint or two on this. Basically, I am trying to create a linked list of records Type "tKante", fill them, one by one with a value, stop the filling when 0 is entered and then iterate over all linked list objects until the last one has its 'next' pointed to nil - so create a singly linked list. Here's the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program learningLinkedListsPainfully;uses crt; typetRefKante = ^tKante;tKante = record           von: integer;           zu: integer;           x:boolean;           next: tRefKante         end; var A : tRefKante;var startA,    lookupPointer : ^tKante; procedure ElementAnhaengen (          inZahlVon : integer;          inZahlZu : integer;          B :boolean;          var ioRefAnfang : tRefKante);        {joins a new object type tRefkante to the linked list}begin  new (A);  if (ioRefAnfang<>nil) then  begin    A^.von :=inZahlVon;    A^.zu := inZahlZu;    A^.x :=B;    A^.next :=ioRefAnfang;    ioRefAnfang := A;  end  else  begin    A^.von :=inZahlVon;    A^.zu := inZahlZu;    A^.x :=B;    ioRefAnfang := A;  end; end; {End of ElementAnhaengen} procedure ShowList (          var ioRefAnfang : tRefKante);var y: ^word;         {iterates over all objects of the linked list}begin  y :=nil;  writeln();  lookupPointer:=ioRefAnfang;  while (lookupPointer<>nil) do  begin    write('Number von: ', lookupPointer^.von,'; ');    write('Number zu: ', lookupPointer^.zu,'; ');    write('X : ', lookupPointer^.x,'; ');    y := addr(lookupPointer);    writeln(',  At Adress: ',y^);    lookupPointer:=lookupPointer^.next;  end; end; {End of ShowList} procedure createList (var ioStartList:tRefKante);var input: integer;    xInt, von, zu: integer;    x: boolean;      {Fills the linked List trough user input and the procedure ElementAnhaengen}Begin  ioStartList:=nil;  repeat  begin      write('Enter "von": ');      read(von);      write('Enter "zu": ');    read(zu);      write('Enter "x": ');      read(xInt);      if (xInt=1) then        x := true      else        x :=false;      ElementAnhaengen(von, zu,x,ioStartList);      writeln('Do again? 0 to Stop');      readln(input);  end  until (input=0); end;    begin   createList(startA);  showlist(startA); readkey();end. The createlist() procedure seems to work as I would expect it to, but the showlist() iterates trough all elements, created by createlist(), and instead of stopping at the last one, tries to go one further... Whisch leads to a SIGSEGV error, see the srceenshot attached. It seems the code tries to follow a link to the next object, even though the object was not created. I would expect a .next Entry in the last record of the linked list to be NIL, because I didn't set it, but it seems its actually pointing at some address that isn't NIL, when I add a simple writeln to check it.. Sorry for a probably super-duper easy question, the search was stronger then me =) ---------------------------------------------------------------------- dbannon · 2021-11-09 23:49 You are not setting 'next' the first time you add an element to the list. I used to love LL when I played with C.  But now you are walking in the light, maybe consider fplist or even avllist ? Davo ---------------------------------------------------------------------- nasprin · 2021-11-10 00:10 Quote from: dbannon on November 10, 2021, 12:49:49 amYou are not setting 'next' the first time you add an element to the list. I used to love LL when I played with C.  But now you are walking in the light, maybe consider fplist or even avllist ? Davo Well, I knew it was easy, but yeah, I didn't think it was that obvious, now that you've said it.. Thanks a lot, that really helped! The linked lists are more a part of my study, but thanks for the advice on other list types =) Hopefully, some day I will be spending my coding time not trying to invent bicycles..