How to fix Access Violation at address in Delphi

2019-09-26 08:05发布

问题:

I'm new to delphi and am trying to start OOP. However I get an access violation when using public property to set a private field.

type
  User = class;
  TData = class
  private
    CurrUser: User;
    Connection: TFDConnection;
    Query: TFDQuery;

  procedure SetUser(newUser: User);
  procedure SetConnection(newConn: TFDConnection);
  procedure SetQuery(newQry: TFDQuery);
public
  property CUser: User read CurrUser write SetUser;
  property Conn: TFDConnection read Connection write SetConnection;
  property Qry: TFDQuery read Query write SetQuery;

  class procedure Login(uID: integer); static;
  class procedure Logout(uID: integer); static;
  class procedure ExitApp(); static;
end;
implementation

{$R *.fmx}

procedure TData.SetUser(newUser: User);
begin
  CurrUser := newUser;
end;

procedure TData.SetConnection(newConn: TFDConnection);
begin
  Connection := newConn;
end;

procedure TData.SetQuery(newQry: TFDQuery);
begin
  Query := newQry;
end;

I expect to be able to set the Connection using that property however it gives me the access violation with any code that uses the property write: TData.Conn.LoginPrompt := False; TData.Conn.Connected := True;

var
  TData: frmData.TData;
  LoginForm: TLoginForm;
  ErrorCount : integer;

implementation

{$R *.fmx}

procedure TLoginForm.ExitAppButtonClick(Sender: TObject);
begin
  TData.ExitApp;
end;

procedure TLoginForm.LoginButtonClick(Sender: TObject);
var
  companyPath : string;
  nurseID : integer;
begin
  if(UsernameInput.Text = '') or (PasswordInput.Text = '') or (PincodeInput.Text = '') then
  begin
    ShowMessage('Please enter your login details.');
    Exit;
  end;

  try
    TData.Conn := TFDConnection.Create(nil);
    TData.Conn.Params.DriverID := 'MSAcc';
    TData.Conn.Params.Database := 'D:\PulseDB\AlfaPersonnel\Pulse.mdb';
    TData.Conn.LoginPrompt := False;
    TData.Conn.Connected := True;
    if(TData.Conn.Connected <> True) then
    begin
      ShowMessage('Could not connect, try again');
      Exit;
    end
    else //When Connection
    begin
      TData.Qry := TFDQuery.Create(TData.Conn);
      try
        TData.Qry.Connection := TData.Conn;
        TData.Qry.SQL.Text := 'SELECT * FROM NurseLogin WHERE Username=:uname AND Password=:pword AND PinCode=:pin;';
        TData.Qry.Params.ParamByName('uname').AsString := UsernameInput.Text;
        TData.Qry.Params.ParamByName('pword').AsString := PasswordInput.Text;
        TData.Qry.Params.ParamByName('pin').AsString := PincodeInput.Text;
        TData.Qry.Active := True;

        if TData.Qry.RecordCount = 0 then ShowMessage('Details not recognised.')
        else if TData.Qry.RecordCount = 1 then
             begin
               if TData.Qry.FieldByName('IsActive').AsBoolean then //If the user is active
               begin
                 try
                   //Connect to the path
                   companyPath := TData.Qry.FieldByName('CompanyName').AsString;
                   TData.Conn.Params.Database := 'D\PulseDB\' + companyPath + '\Pulse.mdb';
                   TData.Conn.Connected := True;

                   ShowMessage('Connected to ' + companyPath);
                 finally

                 end;
               end;
             end;
      finally
      end;
    end;
  finally
  end;
end;

回答1:

You don't ever create an instance of your TData class. At some point you need to write:

TData := frmData.TData.Create;

Which is how you instantiate an instance. And you need to destroy it when you are finished also. Like this:

TData.Free;

That you did not instantiate an instance is the explanation for your access violation.

Some other issues:

  1. Use the T prefix for types. Your variable should be named Data rather than TData.
  2. Don't use global variables if at all possible.


标签: oop delphi