I am trying following code to create a simple GUI application:
program RnTFormclass;
{$mode objfpc}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, Forms, StdCtrls;
type
RnTForm = class(TForm)
private
wnd: TForm;
btn: TButton;
public
constructor create;
procedure showit;
end;
constructor RnTForm.create;
begin
Application.Initialize;
wnd := TForm.Create(Application);
with wnd do begin
Height := 300;
Width := 400;
Position:= poDesktopCenter;
Caption := 'LAZARUS WND';
end;
btn := TButton.Create(wnd);
with btn do begin
SetBounds(0, 0, 100, 50);
Caption := 'Click Me';
Parent := wnd;
end;
end;
procedure RnTForm.showit;
begin
wnd.ShowModal; {Error at this line: Throws exception External: SIGSEGV }
end;
var
myform1: RnTForm;
begin
myform1.create;
myform1.showit;
end.
However, it is throwing exception as mentioned as a comment in code above. Where is the problem and how can it be solved?
myform1.Create
should be myform1 := RnTForm.Create
.
In your code above, myform1
is a nil
pointer (since it is a global variable, it is initialized to nil
) until you assign something to it, in this case a (pointer to a) new instance of a RnTForm
.
And of course, if myform1
is a nil
pointer, you cannot use it as if it was indeed pointing to an object (so myform1.showit
will not work).