如果IMG初始化时就赋值为NIL,那么就非常好判断了。
IF IMG = NIL THEN
  IMG := TBITMAP.CREATE;
否则,就比较难判断了。

解决方案 »

  1.   

    使用函数
    If Not Assigned(Bitmap变量) Then Bitmap变量 := TBitmap.Create;
      

  2.   

    在建立对象并free以后一定要设定未nil,例如下面的代码:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      x:TButton;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);begin
      x:=TButton.Create(self);
      x.parent:=self;
      x.free;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if assigned(x) then
        x.caption:='Error';
    end;end.在点击button1建立对象并释放后点击Button2会出现错误,这是因为对象free后没有设定
    未nil
      

  3.   

    try
      img.classinfo;
    except
      showmessage('not created');