Note that there are some explanatory texts on larger screens.

plurals
  1. PObutton array NullReferenceException was unhandled
    primarykey
    data
    text
    <p>First of all i'm new in here and new with csharp. just exhausted while doing some exercises for learning csharp. cant find a solution why i get the error "NullReferenceException was unhandled" and </p> <p>1) How to overcome this? according to my researches its related with initialisation but couldnt make it. i debug and watched the values all buttons gets gets null values is it because of this? how can i solve it? what to do? which code to add? </p> <p>(***all informing knowledge about class initialisations and arrays and null stuffs are welcome. i wanna learn all the points and wanna be expert :P and people can optimize the code as well. all extra informations are welcome.)</p> <p>2) And why compiler doesnt show error but error comes while running the code?</p> <p>ok now i have the code blow</p> <pre><code>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; namespace temp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Button[] btn = new Button[5]; for (int i = 0; i &lt; 5; i++) { btn[i] = new Button(); btn[i].Width = 50; btn[i].Height = 50; flowLayoutPanel1.Controls.Add(btn[i]); btn[i].Click += new EventHandler(btn_Click); } } void btn_Click(object sender, EventArgs e) { Button[] btn = sender as Button[]; btn[3].Text = "button name changed"; // here problems occurs //btn[3].BackColor = Color.Red; // here problems occurs // btn[3].PerformClick(); // here problems occurs } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload