arrays - 尝试访问数组时不断出现访问冲突

标签 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.

最佳答案

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

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

  1. 根本忘记实例化对象。
  2. 通过在未初始化的实例而不是类型上调用构造函数来错误地实例化对象。

为了说明后一个错误,它看起来像这样:

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

但正确的做法是这样的:

General := TGeneral.Create;

关于arrays - 尝试访问数组时不断出现访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28340978/

相关文章:

javascript - 如何根据输入值数组返回对象数组的匹配结果

Java - 查找多个数组中的重复条目

android - 推送通知 GCM DELPHI XE8

c# - 在没有外部库的情况下在 C# 中播放动态创建的简单声音

c - 调用 ld-2.13.so 中的 _dl_open

javascript - 检查javascript中的二维矩阵中是否存在字符串?

arrays - 修改 MongoDB 中数组的最后一个元素

delphi - TDBNavigator背景透明度颜色错误

Delphi - 在运行时动态添加所有字段在数据集中生成重复项

c# - 错误 : An expression tree may not contain a dynamic operation