Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TStringList access violation

164 views
Skip to first unread message

Peter Jukel

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
Hi

I have a TStringList variable in a unit in the private section of a form
class. I want to add some strings to it in the OnCreate event of the form,
but it generates an access violation. The unit compiles without any errors
or hints. What am I doing wrong? I'm using Delphi 5 Pro.

type
TFormMain = class (TForm);
...
private
FieldTypeList: TStringList
...

procedure TFormMain.FormCreate(Sender: TObject);
begin
FieldTypeList.Add ('test');
end;

Thanks

Peter

Domingo Garcia

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
You have declared the variable, but you haven't created the object. You
need to do this:

procedure TFormMain.FormCreate(Sender: TObject);
begin
FieldTypeList := TStringList.Create;


FieldTypeList.Add ('test');
end;

and in the OnDestroy method

procedure TFormMain.FormDestroy(sender: TObject);
begin
FieldTypeList.Free;
end;

Domingo García

In article <81tf0d$sr...@forums.borland.com>,


Sent via Deja.com http://www.deja.com/
Before you buy.

Yorai Aminov (TeamB)

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
On Mon, 29 Nov 1999 11:08:24 +0200, "Peter Jukel"
<pju...@jukesoft.com> wrote:

>Hi
>
>I have a TStringList variable in a unit in the private section of a form
>class. I want to add some strings to it in the OnCreate event of the form,
>but it generates an access violation.

You did not create the TStringList object.

>procedure TFormMain.FormCreate(Sender: TObject);
>begin
FieldTypeList := TStringList.Create;

> FieldTypeList.Add ('test');
>end;

you can free the object in the OnDestroy event.

---
Yorai Aminov (TeamB)
http://ourworld.compuserve.com/homepages/yaminov
(TeamB cannot answer questions received via email.
To contact me for any other reason remove nospam from my address)

Stephan Reichelt

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
Peter Jukel wrote in <81tf0d$sr...@forums.borland.com>:

[AccessViolation]


>type
> TFormMain = class (TForm);
> ...
> private
> FieldTypeList: TStringList

>....

>procedure TFormMain.FormCreate(Sender: TObject);
>begin
FieldTypeList:=TStringList.Create;

try
> FieldTypeList.Add ('test');
finally
FieldTypeList.Free
end;
>end;


So long,
Stephan R.
--
I'm searching for a good signature ...

Chad Jones

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
> I have a TStringList variable in a unit in the private section of a form
> class. I want to add some strings to it in the OnCreate event of the form,
> but it generates an access violation. The unit compiles without any errors
> or hints. What am I doing wrong? I'm using Delphi 5 Pro.

>
> Peter

Because of Delphi's object referencing model, it is necessary to CREATE
an object (not just declare it) before using it. This might seem like a
pain at first but it allows for tremendous flexibility later when
referencing objects.

- Chad

Peter Jukel

unread,
Nov 29, 1999, 3:00:00 AM11/29/99
to
Thanks

Peter

Yorai Aminov (TeamB) <yam...@nospam.trendline.co.il> wrote in message
news:iEtCOHE3=O2EecYjd0...@4ax.com...


> On Mon, 29 Nov 1999 11:08:24 +0200, "Peter Jukel"
> <pju...@jukesoft.com> wrote:
>
> >Hi
> >

> >I have a TStringList variable in a unit in the private section of a form
> >class. I want to add some strings to it in the OnCreate event of the
form,
> >but it generates an access violation.
>

> You did not create the TStringList object.
>

> >procedure TFormMain.FormCreate(Sender: TObject);
> >begin

0 new messages