Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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?

C#
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));

                    }

                }
            }
        }

    }
Posted
Comments
Afzaal Ahmad Zeeshan 9-Oct-15 12:52pm    
Something is null, something is null, something is null. :-)

Find it, give it a value. Problem solved!

1 solution

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

  • 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.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 9-Oct-15 14:40pm    
5ed.
Sergey Alexandrovich Kryukov 9-Oct-15 14:49pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900