Public Group

# I'm getting EAccessViolation...

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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 :(

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by j0sephOk, 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);}

// 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,

##### Share on other sites
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.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009712
• ### Who's Online (See full list)

There are no registered users currently online

×