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
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?
(***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.)
2) And why compiler doesnt show error but error comes while running the code?
ok now i have the code blow
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 < 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
}
}
}