开发者

C# problem with an array of fields

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


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


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


You need to populate the fieldArray

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜