Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NullReferenceException未被处理_C#_Object - Fatal编程技术网

C# NullReferenceException未被处理

C# NullReferenceException未被处理,c#,object,C#,Object,我已经创建了一个表对象。参见代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace GameMac

我已经创建了一个表对象。参见代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameMachineWF
{
    public partial class Form1 : Form
    {
        Label firstClicked = null;
        Label secoundClicked = null;
        int rows;
        int cols;
        Random random = new Random();
        TableLayoutPanel table;

    List<String> icons = new List<string>()
{
"!", "!" , "N", "N", "Y", "Y", "k", "k", 
"," , ",", "b", "b", "w","w", "v", "v" 
};

    public Form1(int rows, int cols)
    {
        InitializeComponent();

        LoadGame(rows, cols);
    }

    public void LoadGame(int rows, int cols)
    {
        TableLayoutPanel table = new TableLayoutPanel();
        table.Dock = DockStyle.Fill;
        table.ColumnCount = cols;
        table.RowCount = rows;
        table.BackColor = Color.CornflowerBlue;

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Label label = new Label();
                label.Dock = DockStyle.Fill;
                label.Name = "label" + i + "" + j;
                label.Text = "c";
                 label.Click += this.label_Click;

                label.Size = new Size(100, 100);
                 label.Font = new Font("Webdings", this.Font.Size);

                label.BorderStyle = BorderStyle.Fixed3D;

                table.Controls.Add(label, i, j);
                table.AutoSize = true;
            }
        }

        this.Controls.Add(table);

        this.Size = new Size(rows * 100 + 50, cols * 100 + 50);

        foreach (Control control in table.Controls)
        {
            Label iconLabel = control as Label;

            if (iconLabel != null)
            {
                int randomNumber = random.Next(icons.Count);
                iconLabel.Text = icons[randomNumber];
                iconLabel.ForeColor = iconLabel.BackColor;
                icons.RemoveAt(randomNumber);
            }
        }
    }

    private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
    {
    }

    private void label_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == true)
            return;

        Label clicklabel = sender as Label;

        if (clicklabel != null)
        {
            if (clicklabel.ForeColor == Color.Black)
            {
                return;
            }

            if (firstClicked == null)
            {
                firstClicked = clicklabel;
                firstClicked.ForeColor = Color.Black;
                return;
            }
        }

        secoundClicked = clicklabel;
        secoundClicked.ForeColor = Color.Black;
        formWinner();

        if (firstClicked.Text == secoundClicked.Text)
        {
            firstClicked = null;
            secoundClicked = null;
            return;
        }

        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        firstClicked.ForeColor = firstClicked.BackColor;
        secoundClicked.ForeColor = secoundClicked.BackColor;
        firstClicked = null;
        secoundClicked = null;
    }

    private void formWinner()
    {
        foreach (Control controls in table.Controls)
        {
            Label lblWin = controls as Label;

            if (lblWin != null)
            {
                if (lblWin.ForeColor == lblWin.BackColor)
                {
                    return;
                }
            }
        }

        MessageBox.Show("You won. Congratulations");
        Close();
    }
}
在FormWinner()方法的tablecontrols元素中取消了NullReferenceException的筛选。


上面的代码是为我正在开发的pairs游戏编写的,该游戏的灵感来自Microsoft pairs game Tutorial for C#。

您是否忘记将单击事件处理程序附加到
LoadGame()中的标签上了

private void x4ToolStripMenuItem_Click(object sender, EventArgs e)
{
        Form1 f1 = new Form1(4, 4);
        f1.ShowDialog();
}
label.Click += this.label_Click;