Access violation Delphi program

2019-09-22 11:25发布

问题:

Please help me with the following code. I get a Access violation from delphi once I click the Log IN button.... Indicated by the !!!

LogIn Unit

unit Log;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,cls_LogIn,Jpeg;

type
  TForm2 = class(TForm)
    lbllogIn: TLabel;
    pnlSplitter: TPanel;
    edtUserName: TEdit;
    edtPassword: TEdit;
    lblUserName: TLabel;
    lblPassword: TLabel;
    btnLogIn: TButton;
    btnRegister: TButton;
    lblRegister: TLabel;
    edtRegName: TEdit;
    edtRegSurname: TEdit;
    edtRegPassword: TEdit;
    edtCountry: TEdit;
    edtHomelanguage: TEdit;
    btnRegisterF: TButton;
    btnCancel: TButton;
    lblRegName: TLabel;
    lblRegSurname: TLabel;
    lblRegPassword: TLabel;
    lblCountry: TLabel;
    lblHomelanguage: TLabel;
    imgLinks: TImage;
    imgRegs: TImage;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnLogInClick(Sender: TObject);
  private

  public

    { Public declarations }
  end;

var
  Form2: TForm2;
  Login : Tlogin;
implementation

uses Main;

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;

end;

procedure TForm2.btnLogInClick(Sender: TObject);   **!!!!!!!!**
var

bUser,bPassword : Boolean;
sUser,sPassword,sMessage : string;
begin
  form1.qrymain.close  ;
Form1.Qrymain.sql.text := 'select * from Gebruikers';
form1.qryMain.open;

sUser := edtUserName.Text;
sPassword := edtpassword.text;
bUser :=  form1.qrymain.locate('Username',sUser,[]);
bPassword := form1.qrymain.locate('Password',sPassword,[]);
Login.create;
login.create(sUser,sPassword,bUser,bPassword);
sMessage:= Login.toetslog;
ShowMessage(sMessage);

end;

Class Unit:

unit cls_LogIn;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, Buttons, math, ExtCtrls;
type
Tlogin = class(Tobject)

private

Fepos :string ;
fwagwoord :string ;
Fbepos : Boolean;
fbwagwoord : Boolean;
public
constructor create; overload;
constructor Create(epos,wagwoord:String;bepos, bwagwoord: boolean); overload; 

function toetslog: string;


end;
implementation

{ Tlogin }



constructor Tlogin.Create(epos, wagwoord: String;bepos, bwagwoord: boolean);
begin
fepos := epos ;
fwagwoord := wagwoord ;
fbepos := bepos;
fbwagwoord := bwagwoord;
end;


constructor Tlogin.create;
begin
 fepos:= ' ';
 fwagwoord := ' ';
 fbepos := false;
 fbwagwoord := true;
end;

function Tlogin.toetslog;
begin

if (fbepos = True) and (fbwagwoord = True)
then
begin
result := 'Welcome '+' '+fepos
 end
else
result := 'SORRY VERKEERD HEHE'+' '+fwagwoord ;
end;


end.
end.

回答1:

Skip login.create and correct next line to :

login := TLogin.Create(sUser,sPassword,bUser,bPassword);

the login object should be freed when the login is ready as well.

Like this :

login := TLogin.Create(sUser,sPassword,bUser,bPassword);
try
  sMessage := login.toetslog;
  ShowMessage(sMessage);
finally
  login.free;
end;