Object reference not set to an instance of an object.-i got null value of checkbox array

Posted by Satiss7pwr under ASP.NET on 1/19/2010 | Views : 4193 | Status : [Member] | Replies : 4
public CheckBox[] cb = null;
int z=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cb = new CheckBox[count - k];
//Database code.........
while (dr.Read())//read data from access database
{
cb[z] = new CheckBox();
cb[z].Text = dr["Member_Name"].ToString();
Panel2.Controls.Add(cb[z]);
Panel2.Controls.Add(new LiteralControl("</br>"));
z = z + 1;
}
}
}
protected void Button6_Click(object sender, EventArgs e)
{
for (int x = 0; x < cb.Length; x++)//ERROR IS HERE:Object reference not set to an instance of an object.
{
//processing check boxes
}
}




Responses

Posted by: RtpHarry on: 1/19/2010 [Member] [MVP] Bronze

Up
0
Down
Consider using the CheckboxList control rather than creating checkbox's on the fly.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx

You problem is that the info in cb has been thrown away by the time you try to use it. It goes like this:

page loads
not post back so code in that block is run and cb is given a value
page finishes loading
html is sent back to visitor
all page data is thrown away
visitor clicks button6
page loads again
code not run because it IS a postback
cb is null / empty
you try to use the data
fails because data is null

So the solution is that you either need to put a checkboxlist on the page so that when you add the checkbox controls to it the checkboxlist will remember what it contains on postback

or

you need to setup the cb field so that it saves its data. Either in viewstate, session, etc:

http://www.developer.com/net/asp/article.php/3606741/ASPNET-Tip-Create-a-ViewState-Property.htm

Satiss7pwr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 1/19/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
This is because whenever a button is clicked it goes to your Page_load first but it cant create CheckBox because it is constrained by
if(!IsPostBack)

Thus when button is clicked, it wouldnt get any checkboxes in your cb variable.

If your intension is to get the checkbox which are there in the webpage, dont use cb which you set as Null during object initialiser rather
Request.Form["your checkbox id"] or search in the Panel2

Because the whole class is initialized again when page is posted back.
:)

www.abhisheksur.com

Satiss7pwr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 1/19/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day

Abhi2434 is telling you the trueth. You are creating a Checkbox on fly, but now if the Condition of is not Postback is not true then the checkbox will not be created and your loop is looking for a checkbox that does not exists because it was not created. its Better to create a Checkbox on design time and use it, it that case its always available.



Thank you for posting at dotnetfunda.

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Satiss7pwr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 1/19/2010 [Member] Bronze

Up
0
Down
Try by removing the not postback check condition.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Satiss7pwr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response