调试出现"access violation at address 00000000",如何解决!
初学的菜鸟,问题多多
[img]http://my.freep.cn/R.asp?U=Dmy/070424/22/0704242219532910_80031.JPG[/img]
Form1为主窗体,当中两个按钮,“打开”按钮调用一个新窗体-Form2;
Form2主要是一个DBGrid,显示数据表格。用ADOQuery,和DataSource作为数据链接控件,在调试的时候第一次运行没有问题,当关闭Form2之后,再次通过Form1的“打开”按钮打开的时候就出现问题了。
[img]http://my.freep.cn/R.asp?U=Dmy/070424/22/0704242219537395_80031.JPG[/img]
运行代码目录下的exe文件也是同样出现此情况
[img]http://my.freep.cn/R.asp?U=Dmy/070424/22/0704242219532881_80031.JPG[/img]
请问这是什么原因导致的,如何解决,谢谢!
========================
//Form1的代码
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Button1: TButton;
procedure BitBtn1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses show, DM_main;
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
form2.ShowModal;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
end.
=================
[DM_main]
unit DM_main;
interface
uses
SysUtils, Classes, DB, ADODB;
type
TDM = class(TDataModule)
ADOConn: TADOConnection;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DM: TDM;
implementation
uses main;
{$R *.dfm}
end.
===================
//Form2的代码!
[show]
unit show;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, DB, ADODB, Grids, DBGrids, ExtCtrls, DBCtrls;
type
TForm2 = class(TForm)
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
DBGrid1: TDBGrid;
Button1: TButton;
DBNavigator1: TDBNavigator;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses DM_main,main;
{$R *.dfm}
procedure TForm2.BitBtn2Click(Sender: TObject);
begin
close;
end;
procedure TForm2.BitBtn1Click(Sender: TObject);
begin
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add( 'select * from tHamlet ');
open;
end;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
with ADOQuery1 do
begin
SQL.Clear;
Close;
end;
end;
end.
[解决办法]
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree; //问题出在这里. 当Form2被Close时,它同时被释放, 于时下次你再Show它时,就需要再Create
end;
//access violation at address 00000000 这样的错误,一般就是对象未创建就使用了.
[解决办法]
在调试的时候第一次运行没有问题,当关闭Form2之后,再次通过Form1的“打开”按钮打开的时候就出现问题了。
---------------------------------
因为你的程序在运行时,已经Create了Form2(请看一下工程文件中,有Application.CreateForm(TForm2, Form2);这一句.)
这时Form2对象已存在,可以点击按钮,执行Form2.Show;让它显示出来.
如前所述,当你关闭Form2时,Form2对象被释放.此时的Form2或者是一个空指针,或者其指针值指向无法预知的地址, 试图对它的操作将引发上述错误.