C# 字段数组问题

发布于 2024-10-26 14:30:02 字数 3848 浏览 6 评论 0原文

我创建了以下代码。

但是,当我运行它时,在这一行>>> 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);
            }
        }
    }
}

I created the following code.

However, when I run it, at this line >> fieldArray[0].label = new Label(); it returns an error saying Object reference not set to an instance of an object. any suggestions to why this happens?

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);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紧拥背影 2024-11-02 14:30:02

该行使

private Field[] fieldArray = new Field[10];

fieldArray 引用一个包含 10 个元素的新数组,其中每个元素可能引用一个 Field。但是,您实际上尚未创建任何 Field 对象,因此每个数组元素都是 null。简而言之:创建数组并不会创建实际的对象。如果你想要 10 个 Field 对象,你需要这样做

for (int i = 0; i < 10; i++)
    fieldArray[i] = new Field();

The line

private Field[] fieldArray = new Field[10];

makes fieldArray refer to a new array with 10 elements, where each element may refer to a Field. However, you haven't actually created any Field objects, so each array element is null. In short: creating an array does not create the actual objects. If you want 10 Field objects, you'll need to do e.g.

for (int i = 0; i < 10; i++)
    fieldArray[i] = new Field();
阳光①夏 2024-11-02 14:30:02

您需要在数组中创建对象

fieldArray = new Field[52]; // just creates the array
fieldArray[0] = new Field(); // creates an object in the array

You need to create the objects in the array

fieldArray = new Field[52]; // just creates the array
fieldArray[0] = new Field(); // creates an object in the array
临风闻羌笛 2024-11-02 14:30:02

您需要填充 fieldArray

for (int i = 0; i < fieldArray.Length; +=i) fieldArray[i] = new Field();

You need to populate the fieldArray

for (int i = 0; i < fieldArray.Length; +=i) fieldArray[i] = new Field();
聚集的泪 2024-11-02 14:30:02

fieldArray[0] 为 null,您必须看看发生了什么,您可能没有初始化数组项:/

fieldArray[0] is null, you must see what happend, you probably did not initialize the array items :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文