access violation when i click button, what is a violation

Go down

access violation when i click button, what is a violation

· #1

i made a second form and when i click the button it says

text
Project project1 raised exception class 'External: SIGSEGV'.

and then a window about access violation. what did i violate. is it legal trouble

Re: access violation when i click button, what is a violation

· #2

No legal trouble. It means your program used something that was not there yet. Can you show the button's code?

Re: access violation when i click button, what is a violation

· #3
pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Label1.Caption := 'hello';
  Form2.Show;
end;

Re: access violation when i click button, what is a violation

· #4

That code is fine. The question is whether Form2 exists when it runs.

Open Project > Project Options > Forms. If Form2 is in the right-hand list ("Available forms") and not the left one ("Auto-create forms"), nobody ever created it. The variable Form2 is nil, and nil.Label1 is your access violation.

Two ways out:

  1. Move Form2 to the auto-create list, or
  2. create it yourself when you need it:
pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Form2 = nil then
    Application.CreateForm(TForm2, Form2);
  Form2.Label1.Caption := 'hello';
  Form2.Show;
end;

Read the error message. Then read it again.

Re: access violation when i click button, what is a violation

· #5

it was in the right list

how did it get in the right list

Re: access violation when i click button, what is a violation

· #6

You put it there. Last Tuesday. There was a topic about it. I locked it.

Forum rules 1 to 10 apply. Yes, to you.

Re: access violation when i click button, what is a violation

· #7

oh yeah

Go up

Sign in to reply