问题
I'm trying to create a component that must use a Bitmap, I'm having a problem when I go to select the image on the property.
Here is an excerpt of the code: Property Declaration
Property StarOff: TBitmap read FStarOff write SetStarOff;
Function SetStarOff
procedure TNotas.SetStarOff(const Value: TBitmap);
begin
FStarOff.Assign(Value);
end;
But, when I assign a value to the property, I get the error:
Access violation at address 1BC324B8 in module 'TNte.bpl'. Read of address 000000000
Why is this happening?
回答1:
Your setter method looks correct, but you're getting an Access Violation because your FStarOff member is nil at the moment.
The usual is to create it at construction time and free it at destruction time.
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited;
FStarOff := TBitmap.Create;
end;
destructor TMyComponent.Destroy;
begin
FStarOff.Free;
inherited;
end;
来源:https://stackoverflow.com/questions/13494829/access-violation-when-assigning-a-value-to-my-component-bitmap-property