With the following program I get Runtime-Error 210 (Object Reference is nil) in line
n:=Memo1.Font.Canvas.TextWidth('...'); unit Unit1;
{$mode objfpc}{$H+}
interface
uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
public
end;
var Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
var n: integer;
begin
Memo1.Lines.Add('Hello');
n:=Memo1.Font.Canvas.TextWidth('Get the width of this text');
Memo1.Lines.Add(IntToStr(n));
end;
end.
I tried it with several Lazarus versions on different OS:
- Windows 7 (32-bit):
Laz 1.8.4/FPC 3.0.4 + Laz 2.0.6/FPC 3.0.4 + Laz 2.1.0 rev=62449 / FPC 3.3.1 rev=43796
- Linux Ubuntu 18.04 (64-bit):
Laz 1.8.4/FPC 3.0.4 + Laz 2.0.6/FPC 3.0.4
Because the explanation for Runtime-Error 210 says:
Object not initialized. When compiled with range checking on, a program will report this
error if you call a virtual method without having called its object’s constructor. I disabled in Project Options / Debugging all 6 checks: -Ci -Cr -Co -Ct -CR -Sa, but this made no difference.
Then I remembered, that I had enabled those Debug checks in fpc.cfg and made a try with the original fpc.cfg (which I had saved), but this made no difference too.
Can somebody reproduce the problem? Do I something wrong? Thanks for your help. I attached my demo.