Search code examples
delphidelphi-2010

Access violation when affecting a value to variable?


The code below is written in unit2 ( form2), it calls the values entered in the email and password boxes ( in form1 ), yesterday the code was working perfectly, i made some changes and now : This code doesn't work, it raises an Access Violation error when i click the Button COMMENCER:

  procedure TForm2.Btn_commencerClick(Sender: TObject);
  begin

  email := form1.ed_Email.Text;// <----- LOOK HERE 
  password := form1.Ed_typedpass.Text; // <-----AND HERE
  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(password)), Length(password));
  password := LowerCase(MD5.AsString);

  etc.......

But this code works :

 email := '[email protected]';
 password := 'mypass'; 
 MD5 := GetMD5;
 MD5.Init;
 etc etc......

The question :

Why ?


Solution

  • Where are you creating your form1 object? Sounds like it haven't initialized before you access it and therefore you get AV.

    Your second code works, because you don't have to initialize string variables before accessing or assigning values to them and you are assigning them directly, not through the form1 variable.

    But breakpoint to email := form1.ed_Email.Text; and look if form1 is nil or not.