对象引用未设置错误2D数组 [英] Object reference not set error 2D array

查看:66
本文介绍了对象引用未设置错误2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为数组中的特定按钮设置名称,但是一旦我尝试运行它,它会向我发送一个错误,即对象引用未设置为对象的实例。我是c#的新手,如何解决这个问题?



I am trying to set a name for a particular button from the array but once i try run it, it would send me an error that goes "Object reference not set to an instance of an object". I am new to c#, how can I solve this problem?

Button[,] btnSeat = new Button[8, 6];
    private void initializeBoard()
    {
        bool cutoff;
        for (int i = 0; i <= 7; i++)
        {
            for (int j = 0; j <= 5; j++)
            {
                cutoff = true;
                if (i == 2 || i == 3 || i == 4 || i == 5)
                {
                    if (j == 4 || j == 5)
                    {
                        cutoff = false;

                    }
                }
                if (cutoff == true)
                {
                    btnSeat[i, j] = new Button();
                    btnSeat[i, j].Width = 80;
                    btnSeat[i, j].Height = 80;

                    btnSeat[i, j].Left = (90 * i);
                    if (i == 2) btnSeat[i, j].Left = (120 * i);
                    if (i == 3) btnSeat[i, j].Left = (110 * i);
                    if (i == 4) btnSeat[i, j].Left = (130 * i);
                    if (i == 5) btnSeat[i, j].Left = (122 * i);
                    if (i == 6) btnSeat[i, j].Left = (125 * i);
                    if (i == 7) btnSeat[i, j].Left = (120 * i);


                    btnSeat[i, j].Top = (90 * j);
                    btnSeat[i, j].Image = Image.FromFile("../../monitor.png");
                    pnlSeat.Controls.Add(btnSeat[i, j]);
                    String buttonName = "btn" + i + j;
                    btnSeat[i, j].Name = buttonName;
                    btnSeat[i, j].Text = buttonName;
                    btnSeat[i, j].Click += new EventHandler(seat_Click);

                    btnSeat[0, 0].Text = "192.168.1.2";
                    btnSeat[0, 1].Text = "192.168.1.5";
                    btnSeat[0, 2].Text = "192.168.1.8"; 
                    if (btnSeat[i, j].Text == clientIP)
                    {
                        btnSeat[i, j].Image = Image.FromFile("../../ol.png");
                        btnSeat[i, j].BeginInvoke(new MethodInvoker(() => btnSeat[i, j].Text = sClientHost));

                    }

                }
            }
        }

    }

推荐答案

调试代码,你会发现问题。特别要注意以下几点:

Debug your code and you'll find the problem. In particular, concentrate on the following lines:
btnSeat[0, 0].Text = "192.168.1.2";
btnSeat[0, 1].Text = "192.168.1.5";
btnSeat[0, 2].Text = "192.168.1.8"; 



  • 您从一个数组开始,所有元素都设置为 null
  • 在循环的第一次迭代中,将 btnSeat [0,0] 设置为新的 Button 实例。
  • 然后尝试在 btnSeat [0,1] btnSeat [0,2]上设置属性,两者仍然是 null

    • You start with an array with all elements set to null.
    • On the first iteration of the loop, you set btnSeat[0, 0] to a new Button instance.
    • You then try to set a property on btnSeat[0, 1] and btnSeat[0, 2], both of which are still null.

    • 这篇关于对象引用未设置错误2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆