0

Hello guys when I click one of these words on Button, I take an error about NullReferenceException was unhandled. look: error image

error image

namespace HangMan { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    string[] words = { "PENCIL", "UMBRELLA", "RUBBER", "RING", "NECKLACE", "SPEED", "PROGRAMMER" };
    private void Form1_Load(object sender, EventArgs e)
    {
        setWords();
        getWords();
    }

    private void getWords()
    {
        Random rnd = new Random();

        string selectedWord = words[rnd.Next(0, words.Length)];

        foreach (char item in selectedWord)
        {
            Label lbl = new Label();
            lbl.AutoSize = false;
            lbl.Width = 25;
            lbl.Height = 25;
            lbl.Text = "_";
            flowLayoutPanel2.Controls.Add(lbl);
        }

    }
    private void setWords()
    {
        for (char i = 'A'; i <= 'Z'; i++)
        {
            Button btn = new Button();
            btn.Text = i.ToString();
            btn.Width = 25;
            btn.Height = 25;
            btn.Click += Btn_Click;

            flowLayoutPanel1.Controls.Add(btn);
        }
    }

    int Score = 0;

    private void Btn_Click(object sender, EventArgs e)
    {

        Button clicked = sender as Button;

        bool IsCharOpened = false;
        foreach (Control item in flowLayoutPanel2.Controls)
        {
            if (item is Label)
            {
                Label lbl = (Label)item; 

                if (lbl.Tag.ToString() == clicked.Text)
                {
                    lbl.Text = clicked.Text;
                    Score += 100;
                    IsCharOpened = true;
                }
            }
        }

        if (IsCharOpened == false)
        {
            lblRemaining.Text = (int.Parse(lblRemaining.Text) - 1).ToString();
            if (int.Parse(lblRemaining.Text) == 0)
            {
                MessageBox.Show("YOU HAVE FAILED..!");
                Application.Exit();
            }
        }

        lblScore.Text = Score.ToString();
        clicked.Enabled = false;
    }
}

}

I cannot find any solution, and I am new on c#. I need your help guys, thanks.

mrcpngl
  • 1
  • 1

3 Answers3

0

Just try this and it should work.

clicked.Text

To this:

clicked.Text.ToString();
0

Just try this and it should work. TO this:

           if (lbl.Tag.ToString() == clicked.Text.ToString)
            {
                lbl.Text = clicked.Text.ToString();

            }
Malakiya sanjay
  • 208
  • 2
  • 12
0

Here....... (lbl.Tag.ToString() == clicked.Text) you have'nt set any tag data lbl.Tag null that's why you are getting null reference exception

Replace your code with

foreach (char item in selectedWord)
        {
            Label lbl = new Label();
            lbl.AutoSize = false;
            lbl.Width = 25;
            lbl.Height = 25;
            lbl.Text = "_";
            lbl.Tag = item;
            flowLayoutPanel2.Controls.Add(lbl);
        }

And in Second Place Replace Code With

foreach (Control item in flowLayoutPanel2.Controls)
        {
            if (item is Label)
            {
                Label lbl = (Label)item; //C# özgü
                                         //Label lbl = item as Label; Label lbl = item as Label; // V.B özgü ama ikiside olur.
                if (lbl.Tag!=null && lbl.Tag.ToString() == clicked.Text)
                {
                    lbl.Text = clicked.Text;
                    Score += 100;
                    IsCharOpened = true;
                }
            }
        }
Samet
  • 1
  • 2