I am having an Access Violation when my project tries to open up a separate form using the .Show command.
The code is supposed to open the login form. But just outputs an Access Violation error.
This is the code from the main form, and is run on its activation:
procedure TForm4.FormActivate(Sender: TObject);
begin
label1.BringToFront;
DBMatch.Enabled := false;
DBContestants.Enabled := false;
btncreate.Enabled := false;
DbNav.Enabled := false;
login.Show;
end;
The below code is the start of the login form:
unit login_form;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs ,StdCtrls, ExtCtrls, unit4,pngimage;
type
Tlogin = class(TForm)
edit_username: TEdit;
edit_password: TEdit;
btnlogin: TButton;
btnForgotten: TButton;
lUsername: TLabel;
LPassword: TLabel;
Imageside: TImage;
procedure btnloginClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
end;
var
login: Tlogin;
password,result:string;
implementation
{$R *.dfm}
uses dmChess_u;
This is one of the errors on the main form

Your
Form4(MainForm) object is being created before theloginobject is created. Chances are, theTForm.OnActivateevent is also being fired before theloginobject is created, too. Thus, theloginpointer would still benilwhen you try to callShowon it. You can easily verify that by using the debugger.A Form's
OnActivateevent is not a good place to display a login form. That event is fired whenever a Form gains input focus, which can happen many times during the Form's lifetime.Also, it is generally best to not auto-create your Form objects at program startup, except for the MainForm. Create your Form objects dynamically via their
Createconstructor only when actually needed, and free them when no longer needed, eg:Alternatively:
However, in this particular case, if you want the user to login before interacting with the MainForm, I would suggest displaying the login form before the MainForm is even created, eg: