I am pretty new at ASP.Net and I'm trying to program a website which creates wanted amount of buttons. I have a textbox and a button, I take the number of button I want from textbox by clicking button, it's simple. And I have this code I wrote. It should have worked but I get a "System.NullReferenceException: Object reference not set to an instance of an object." error at the line where d1[k].ID exists. I searched about this error a little bit and I found out it's because d1[k].ID variable is null, but I don't know what to do. How can I solve this error?
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int Sayi = Convert.ToInt32(TextBox1.Text);
Button[] d1=new Button[Sayi];
int Sy = 20;
for (int k = 1; k <= Sayi; k++) {
d1[k].ID = "Btn" + k.ToString();
d1[k].Width = 100;
d1[k].Height = 25;
d1[k].Text = k.ToString();
d1[k].Attributes.Add("style", "top:"+Sy+"; left:10 ;position:absolute");
Sy += 20;
}
}