Delphi - Access Violation in login form

Asked

Viewed 676 times

1

Guys I have a problem, I’m beginner in Delphi and I am developing a registration and consultation system, it is almost finished, I am doing the login part of the system, where only registered users will have access.

But by entering the user and password and clicking login is giving me the following error

Raised Exception class $C0000005 with message - Acess Violation at 0x008ebfd5: read of address 0x000001a0

Access Violation
Could someone give me a hand there? I searched and saw that this error is caused when using an object that has not been instantiated or that has already been destroyed, I debugged and it returns me the line 37 that contains:

with Dtm.fdquery_Login do

unit Unt_Login;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;

type
  Tfrm_Login = class(TForm)
    edt_Usuario: TEdit;
    Label1: TLabel;
    edt_Senha: TEdit;
    Label2: TLabel;
    bbtn_Entrar: TBitBtn;
    bbtn_Cancelar: TBitBtn;
    procedure bbtn_EntrarClick(Sender: TObject);
    procedure edt_SenhaChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frm_Login: Tfrm_Login;

implementation

{$R *.dfm}

uses
  Unt_Dtm;

procedure Tfrm_Login.bbtn_EntrarClick(Sender: TObject);
begin
  with dtm.fdquery_Login do
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT * FROM tb_Usuario WHERE user_login = :login AND user_senha = :senha');
    ParamByName('login').AsString := edt_Usuario.Text;
    ParamByName('senha').AsString := edt_Senha.Text;
    Open;

    if RecordCount > 0 then
    begin
      if (Time >= StrToTime('00:00:00')) and (Time < StrToTime('11:59:59')) then
      begin
        Application.MessageBox('Olá :bom_dia_nome_usuario bom dia, seu acesso ao sistema foi liberado', 'Seja bem vindo(a)', MB_OK);
        ParamByName('bom_dia_nome_usuario').Value := dtm.fdquery_Loginuser_nome.AsString;
      end;

      if (Time >= StrToTime('12:00:00')) and (Time < StrToTime('17:59:59')) then
      begin
        Application.MessageBox('Olá :boa_tarde_nome_usuario boa tarde, seu acesso ao sistema foi liberado', 'Seja bem vindo(a)', MB_OK);
        ParamByName(':boa_tarde_nome_usuario').Value := dtm.fdquery_Loginuser_nome.AsString;
      end;

      if (Time >= StrToTime('18:00:00')) and (Time < StrToTime('23:59:59')) then
      begin
        Application.MessageBox('Olá :boa_noite_nome_usuario boa noite, seu acesso ao sistema foi liberado', 'Seja bem vindo(a)', MB_OK);
        ParamByName(':boa_noite_nome_usuario').Value := dtm.fdquery_Loginuser_nome.AsString;
      end;

      frm_Login.Close;

    end

    else
    begin
      Application.MessageBox('Usuário ou Senha inválidos', 'Dados inválidos', MB_OK + MB_ICONERROR);
      Exit;
    end;

  end;

end;

procedure Tfrm_Login.edt_SenhaChange(Sender: TObject);
begin
  edt_Senha.PasswordChar := '*';
end;

end.

1 answer

2


This problem occurs because dtm.fdquery_Login was not created, does not exist, is equal to nil.

In Delphi objects must be instantiated before being used.

if dtm = nil then
  dtm := Tdtm.Create(Self);

Now this is also true if the objects were not added in the form, those that were declared as variables should also be instantiated.

dtm.fdquery_Login := NomeDaClasse.Create(Self);

And only after this, use the with dtm.fdquery_Login do

  • Thank you very much for the answer Junior, it was very helpful!

  • 1

    I recommend not using with, at first glance can even save on typing but on a procedure great Delphi may lose reference and including the dtm.fdquery_Login explicitly clarifies the code for future change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.