Я пытаюсь создать 2D -массив классов. Под каждым классом 2 массива и их размеры. Этот класс будет дефолт по умолчанию функции BuildhighlightedAttrib (), чтобы быть доступной по всей форме. Я почти уверен, что все делаю правильно, но я продолжаю получать эту ошибку: < /p>
system.nullreferenceexexception: «Справочник по объекту не установлен на экземпляр объекта». воспроизводимый пример. Я получаю ошибку в строке: < /p>
highlightedAttrb[(uint)Role.GOALKEEPER][DEFEND].Key[0] = (uint)Goalkeeping.AERIAL_REACH;
< /code>
form1.cs
namespace MyApp
{
public partial class Form1 : Form
{
public const uint GOALKEEPER = 0;
public const uint DEFEND = 0;
public const uint SUPPORT = 1;
public const uint ATTACK = 2;
public const uint NUM_ROLES = 45;
public enum Goalkeeping
{
AERIAL_REACH = 0,
COMMAND_OF_AREA,
COMMUNICATION,
ECCENTRICITY,
FIRST_TOUCH,
HANDLING,
KICKING,
ONE_ON_ONES,
PASSING,
PUNCHING,
REFLEXES,
RUSHING_OUT,
THROWING,
SPARE /* One less attribute for GKs */
}
private MyClass[][] class1 = new MyClass[NUM_ROLES][];
public Form1 ()
{
InitializeComponent();
BuildHighlightedAttrb();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BuildHighlightedAttrb()
{
/* Goalkeeper - Defend */
class1[GOALKEEPER][DEFEND] = new MyClass(6, 1);
class1[GOALKEEPER][DEFEND].Key[0] = (uint)Goalkeeping.AERIAL_REACH;
class1[GOALKEEPER][DEFEND].Key[1] = (uint)Goalkeeping.COMMAND_OF_AREA;
class1[GOALKEEPER][DEFEND].Key[2] = (uint)Goalkeeping.COMMUNICATION;
class1[GOALKEEPER][DEFEND].Key[3] = (uint)Goalkeeping.HANDLING;
class1[GOALKEEPER][DEFEND].Key[4] = (uint)Goalkeeping.KICKING;
class1[GOALKEEPER][DEFEND].Key[5] = (uint)Goalkeeping.REFLEXES;
class1[GOALKEEPER][DEFEND].Pref[0] = (uint)Goalkeeping.THROWING;
}
}
/* Classes */
public class MyClass
{
public MyClass(uint numKey, uint numPref)
{
this.Key = new uint[numKey];
this.Pref = new uint[numPref];
this.NumKey = numKey;
this.NumPref = numPref;
}
public uint[] Key { get; set; }
public uint[] Pref { get; set; }
public uint NumKey { get; set; }
public uint NumPref { get; set; }
}
}
< /code>
функциональныйnamespace TestApp
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... inside-a-c