我创建了以下代码。
然而,当我运行它时,在此行>> fieldArray[0].label = new Label();
它返回一个错误,指出对象引用未设置为对象的实例。为什么会发生这种情况的任何建议?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Field[] fieldArray = new Field[10];
public Form1()
{
InitializeComponent();
}
private void populateTree(string path, TreeNode tv1)
{
string[] dir = Directory.GetDirectories(path);
foreach (string d in dir)
{
string entry = Path.GetFileName(d);
TreeNode t = tv1.Nodes.Add("Folder", entry, 0);
populateTree(d, t);
}
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
string entry = Path.GetFileName(f);
tv1.Nodes.Add(f, entry, 1);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//populate the tree
TreeNode t = treeView1.Nodes.Add("Units");
populateTree(@"..\units\", t);
fieldArray[0].label = new Label();
fieldArray[0].label.AutoSize = true;
fieldArray[0].label.Location = new System.Drawing.Point(323, 9);
fieldArray[0].label.Name = "Programtittle";
fieldArray[0].label.Text = "UAI UnitDef Editor";
this.Controls.Add(fieldArray[0].label);
int clabel = 36;
//fieldArray[1].varName = new string[] { "unitName", "name", "description" }; //define labels
//popluate label
for (int i = 1; i < fieldArray[i].varName.Length; i++)
{
fieldArray[i].label = new Label();
fieldArray[i].label.AutoSize = true;
fieldArray[i].label.Location = new System.Drawing.Point(323, clabel);
fieldArray[i].label.Name = "label";
this.Controls.Add(fieldArray[i].label);
fieldArray[i].label.Text = fieldArray[i].varName[i];
clabel = clabel + 26;
}
//populate textbox
int cbox = 33;
for (int i = 0; i < fieldArray[i].varName.Length; i++)
{
fieldArray[i].txtBox = new TextBox();
fieldArray[i].txtBox.Location = new System.Drawing.Point(380, cbox);
fieldArray[i].txtBox.Name = "txtBox";
fieldArray[i].txtBox.Size = new System.Drawing.Size(100, 50);
this.Controls.Add(fieldArray[i].txtBox);
cbox = cbox + 26;
}
}
private void populateLabelTxtBox(string path)
{
//f.txtBox.Multiline = true; //added for testing purpose;
//read,split file
string text = System.IO.File.ReadAllText(path);
char[] delimiters = new char[] { '{', '=', ';', '}' };
string[] parts = text.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
fieldArray[i].txtBox.Text = parts[i];
}
}
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeView1.SelectedNode.Name != "Folder")
{
string text = System.IO.File.ReadAllText(treeView1.SelectedNode.Name);
//f.txtBox.Text = text;
populateLabelTxtBox(treeView1.SelectedNode.Name);
}
}
}
}
答案 0 :(得分:4)
该行
private Field[] fieldArray = new Field[10];
使fieldArray
引用包含10个元素的新数组,其中每个元素可能引用Field
。但是,您实际上并未创建任何Field
对象,因此每个数组元素都是null
。简而言之:创建数组不会创建实际对象。如果您想要10个Field
个对象,则需要执行此操作。
for (int i = 0; i < 10; i++)
fieldArray[i] = new Field();
答案 1 :(得分:0)
您需要在数组中创建对象
fieldArray = new Field[52]; // just creates the array
fieldArray[0] = new Field(); // creates an object in the array
答案 2 :(得分:0)
您需要填充fieldArray
for (int i = 0; i < fieldArray.Length; +=i) fieldArray[i] = new Field();
答案 3 :(得分:-1)
fieldArray [0]为空,你必须看到发生了什么,你可能没有初始化数组项:/