Skip to main content
GameDev.net gamedev.net
🔒 Locked

I'm getting EAccessViolation...

Started by Q-Parser Jun 16, 2005 at 6:23 PM 4 replies 2.1k views
Original Post
Q-Parser
Q-Parser
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.
Raduprv
Raduprv
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.
Q-Parser
Q-Parser
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 :(
Q-Parser
Q-Parser
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.
Emmanuel Deloget
Emmanuel Deloget
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,
auLucifer
auLucifer
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.