I'm getting EAccessViolation...

Started by
4 comments, last by auLucifer 18 years, 10 months ago
Hi! I'm coding a school project and I've stuck with this error. I'm getting EAccessViolation when I'm trying to access Caption property of ListView in Form1 from within Form2. I don't just get it as Unit2 can "see" Form1, I mean I have the Unit1 header included in Unit2. Just a sample for illustration: Form1->ListView1->Items->Item[0]->Caption=Form2->Edit3->Text; I looked in the help what this error message means but it didn't provide me with a single clue what I have done wrong. Thanks for your help. PS: I'm using C++ Builder, if it is any help.
Advertisement
It means that you are trying to access a piece of memory at an invalid location.
So either one (or both) of:
Form1->ListView1->Items->Item[0]->CaptionForm2->Edit3->Text;

is invalid.
More likely the first one but I have no clue how to solve it. I don't understand why I can interact with Form2 objects from within Form1 and not vice versa :(
Ok, I've got another one. But I can't understand this:
FILE *f;AnsiString temp;fopen("file.db","r");fscanf(f, "%s",&temp);fclose(f);


The content of file.db is just "string", nothing more.
Quote:Original post by j0seph
Ok, I've got another one. But I can't understand this:
FILE *f;AnsiString temp;fopen("file.db","r");fscanf(f, "%s",&temp);fclose(f);


The content of file.db is just "string", nothing more.


This one is easier: f is not initialized. fopen() returns a new FILE pointer.
FILE *f;f = fopen("file.db","r");

Then, you need to test f (if the file is not found or cannot be opened, fopen() returns NULL)
if (f){  fscanf(f, "%s",&temp);  fclose(f);}


About your first one: you may find where is your error by adding tests on each pointer:
// Form1->ListView1->Items->Item[0]->Captionif (Form1){  if (Form1->ListView1)  {    .. and so on  }}

You notice: it looks bad. OTOH, you should not gain access to Caption ths way - it is far better to encapsulate the whole thing (create a method in your form class that calls the correct method of your ListView object - I can't believe there's no method to set the Caption value of the first item).

HTH,
Do you have form2 on autocreate?
If you have removed the form from being created automatically in the projects properties then you will get this exception.
Also does the listview actually have items in it?
You cannot access item[0] unless you have done an AddItem(String) or added items at design time.

Also when using files in CBuilder look inhelp for TFileStream
You might find that easier to use.

This topic is closed to new replies.

Advertisement