尝试访问阵列时继续获取访问冲突

时间:2015-02-05 09:58:35

标签: arrays delphi dynamic access-violation

我是delphi的新手,当我尝试访问我的数组“nieuwButtons”时,我一直遇到访问冲突。有谁知道我做错了什么?

unit UGeneral;

  interface

  uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
    System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, SMDBGrid,
    KJSMDBGrid, Vcl.ExtCtrls, KJPanel, Vcl.StdCtrls, Vcl.Mask, Vcl.DBCtrls,
    Data.DB;

  type
    TGeneral = class(TObject)

    private
      { Private declarations }

    public
      nieuwButtons: array of TButton;
      nieuwButtonsDataSource: array of TDataSource;

      procedure listEdits(x, y: Integer; owner: TComponent; parent: TWinControl;
        source: TDataSource);
      procedure nieuwClick(Sender: TObject);
      { Public declarations }
    end;

  var
    General: TGeneral;

  implementation

  procedure TGeneral.nieuwClick(Sender: TObject);
  var
    i: Integer;

  begin
    for i := 0 to Length(nieuwButtons) - 1 do
    begin
      if (nieuwButtons[i] = Sender) then
      begin
        nieuwButtonsDataSource[i].DataSet.Insert;
      end;
    end;
  end;

  procedure TGeneral.listEdits(x, y: Integer; owner: TComponent;
    parent: TWinControl; source: TDataSource);
  var
    i: Integer;
    edit: TDBEdit;
    _label: TLabel;
    biggestWidth: Integer;
    button: TButton;
    edits: array of TDBEdit;
    index: Integer;
  begin
    if nieuwButtons <> nil then //I get an access violation here
    begin
      SetLength(General.nieuwButtons, 0);
      SetLength(nieuwButtonsDataSource, 0);
    end;
    index := Length(nieuwButtons);
    SetLength(nieuwButtons, index + 1);
    SetLength(nieuwButtonsDataSource, index + 1);
    button := TButton.Create(owner);
    button.parent := parent;
    button.Top := y;
    button.Left := x;
    button.Caption := 'Nieuw';
    button.OnClick := nieuwClick;

    nieuwButtons[index] := button;
    nieuwButtonsDataSource[index] := source;

    biggestWidth := 0;
    SetLength(edits, source.DataSet.Fields.Count);
    edit := TDBEdit.Create(owner);
    for i := 0 to source.DataSet.Fields.Count - 1 do
    begin
      _label := TLabel.Create(owner);
      _label.parent := parent;
      _label.Caption := source.DataSet.Fields[i].FieldName;
      _label.Top := ((i * 22) + 30 + y); // 22 = standaard(?) hoogte van edittext
      _label.Left := x;

      _label.Show;

      if _label.Width > biggestWidth then
      begin
        biggestWidth := _label.Width;
      end;
    end;
    i := 0;
    for i := 0 to source.DataSet.Fields.Count - 1 do
    begin
      edit := TDBEdit.Create(owner);
      edit.parent := parent;
      edit.DataField := source.DataSet.Fields[i].FieldName;
      edit.DataSource := source;
      edit.Top := ((i * edit.Height) + 30 + y);
      edit.Left := biggestWidth + 30;
      edits[i] := edit;
      edit.Show;
    end;

  end;

end.

1 个答案:

答案 0 :(得分:5)

该行代码引发异常的唯一方法是TGeneral的实例无效。

错误将在实例化类的代码中找到。此错误的常见形式是:

  1. 忘记实例化对象。
  2. 通过在未初始化的实例上调用构造函数而不是类型来错误地实例化对象。
  3. 为了说明后一种错误,它看起来像这样:

    var
      General: TGeneral;
    ....
    General.Create; // sometimes this way
    General := General.Create; // or sometimes this way
    

    但正确的方法是这样的:

    General := TGeneral.Create;