Data to an object list. Get run time error.

Hello Experts,
 
   I have a problem with a new project. I have a area I call common. I put all my objects and global variables here. One of these objects is called TOfficer. I use this object to hold a list of officers. When I call LoadOfficerObj  from my main form it get this error: "project gym.exe raised exception class EAccessViolation with message "Access viololation at address ect..."
   
   Here is my procedure that is having the problem. I highlighted the line it bombs on. Any ideas are welcome as I am stuck.

procedure TGymMgr.LoadOfficerObj;
var
   oOff : TOfficer;
begin
   qryGym.Sql.Clear;
   qryGym.Sql.Add ( 'SELECT EMP, LN, FN, MN, NSFX, DOB, ENTRY, RMODIFY' );
   qryGym.Sql.Add ( 'FROM PERS.EMP' );
   qryGym.Sql.Add ( 'Where Emp is not null' );
   qryGym.Sql.Add ( 'Order by LN, FN' );
   qryGym.SQL.SaveToFile('H:\Gym.sql');
   qryGym.Open;
   qryGym.first;

   while not qryGym.eof do begin
     oOff := TOfficer.Create;
     oOff.EmpId           := qryGym.FieldByName('EMP').AsString;
     oOff.LastName    := qryGym.FieldByName('LN').AsString;
     oOff.FirstName     := qryGym.FieldByName('FN').AsString;
     oOff.MiddleName  := qryGym.FieldByName('MN').AsString;
     oOff.NameSuffix  := qryGym.FieldByName('NSFX').AsString;
     oOff.DOB              := qryGym.FieldByName('DOB').AsString;
     oOff.Entry             := qryGym.FieldByName('ENTRY').AsString;
     oOff.Rmodify         := qryGym.FieldByName('RMODIFY').AsString;
     oOff.ObjStatus       := OK;
     oOff.ObjState        := ExistingObject;
     Officer.Add ( oOff );      //////////////////////this where it blows up
     qryGym.Next;
   end;
   qryGym.Close;
end;

thank you for looking,
MrError
MrErrorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
Have you created Officer?

ie: Officer := TOfficer.Create(...)?

Cheers,

Raymond.
0
rwilson032697Commented:
I guess it wont be TOfficer!

Anyway, it looks like Officer has not been created.

Cheers,

Raymond.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrErrorAuthor Commented:
Raymond,

   Ahh I think I do create it. Look for these two line in my initial posting...if I am missing something,  please correct me.

  while not qryGym.eof do begin
         oOff := TOfficer.Create;

Thanx,
David
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

rwilson032697Commented:
Its this line I mean:

     Officer.Add ( oOff );      //////////////////////this where it blows up

Where is 'Officer' created?

Cheers,

Raymond.
0
MrErrorAuthor Commented:
Raymond,

  Ahh do you mean this? I call this on startup.
 
constructor TGymMgr.Create;
begin
  Inherited Create;
  Officer   := TList.Create;
  Aerobic   := TList.Create;
  Physlab   := TList.Create;
  Reach     := TList.Create;
  Situp        := TList.Create;
  Smoke     := TList.Create;
  Strength  := TList.Create;
  Stress    := TList.Create;
end;

Confused,
David
0
rwilson032697Commented:
Put a breakpoint on the line that gives the error. Inspect the value of Officers - what is it?

Also, make sure that 'officers' is not freed somewhere unexpectedly :-)

Cheers,

Raymond.
0
MrErrorAuthor Commented:
Raymond,

 When I run the program and set the break point to where it bomb then I move my mouse over  ( oOff ) and I get this:


oOff = (fOID:''; fState:ExistingObject; fStatus:OK; fEmpId:'325'; fLastName:'ABEL';
fFirstName:'STEVE'; fMiddleName:'ROBERT'; NameSuffix:''; fDOB:'12/23/45';
fEntery:'12/17/95 6:38:36 AM'; fRmodify:'')

   Your comment "make sure that 'officers' is not freed somewhere unexpectedly".  I have been looking but found nothing yet I will look some more.

Thank you again,
David
0
rwilson032697Commented:
Sorry, I meant the object called 'Officer' no 'oOff'.

Cheers,

Raymond.
0
deroverCommented:
Like the other guys who commented on this, I'm pretty sure the TList (Officer) is freed, or just not created. You could test this by defining and creating a tmp TList (or post ALL your code, so we can check that out :)) IF it does not crash anymore, you really freed, did not create, or maybe even reassigned the TLists pointer (officer).  If this still crashes, the problem is prolly not the TList 'Officer'.   Sometimes delphi "says" it crasht somewhere, where in fact it didn't crash at all (huh ? :).   Btw, did you "inherited Create"  in the TOfficer.Create ?


Here's the 'tmpList' code :


procedure TGymMgr.LoadOfficerObj;
var
   oOff : TOfficer;
   tmpList:TList;
begin
   qryGym.Sql.Clear;
   qryGym.Sql.Add ( 'SELECT EMP, LN, FN, MN, NSFX, DOB, ENTRY, RMODIFY' );
   qryGym.Sql.Add ( 'FROM PERS.EMP' );
   qryGym.Sql.Add ( 'Where Emp is not null' );
   qryGym.Sql.Add ( 'Order by LN, FN' );
   qryGym.SQL.SaveToFile('H:\Gym.sql');
   qryGym.Open;
   qryGym.first;


   tmpList:=TList.Create;

   while not qryGym.eof do begin
     oOff := TOfficer.Create;
     oOff.EmpId           := qryGym.FieldByName('EMP').AsString;
     oOff.LastName    := qryGym.FieldByName('LN').AsString;
     oOff.FirstName     := qryGym.FieldByName('FN').AsString;
     oOff.MiddleName  := qryGym.FieldByName('MN').AsString;
     oOff.NameSuffix  := qryGym.FieldByName('NSFX').AsString;
     oOff.DOB              := qryGym.FieldByName('DOB').AsString;
     oOff.Entry             := qryGym.FieldByName('ENTRY').AsString;
     oOff.Rmodify         := qryGym.FieldByName('RMODIFY').AsString;
     oOff.ObjStatus       := OK;
     oOff.ObjState        := ExistingObject;


     tmpList.Add ( oOff );      //This really should work...


     qryGym.Next;
   end;
   qryGym.Close;


   // you should FREE the tmpList here, and its items (first)... but you know, its just a test :P


end;
0
MrErrorAuthor Commented:
rwilson,

  Ahhh yup you are correct I forgot to create the list. Doe! Well thank you all for the help.

David
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.