Click here to Skip to main content
16,016,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created an array of RadioButtonList class, but apparently can't seem to access it or use the answer retrieved from it.

I always get the exception: Object reference not set to an instance of an object.
protected void Button5_Click(object sender, EventArgs e)
{


    RadioButtonList[] RBLPain = new RadioButtonList[2];





        RBLPain[0] = new RadioButtonList();


        RBLPain[0].Items.Add("Yes");

        RBLPain[0].Items.Add("No");

        Panel1.Controls.Add(RBLPain[0]);



            if (RBLPain[0].SelectedItem.Text == "Yes")
            {
               Response.Write(RBLPain[0].SelectedItem.Text);
            }


    }



Any help would be appreciated thanks :)
Posted
Updated 13-Mar-12 16:02pm
v3

1 solution

you can alter your code like this , add a judgment :
C#
ifRBLPain[0].SelectedItem != null)
{
  if (RBLPain[0].SelectedItem.Text == "Yes")
   {
       Response.Write(RBLPain[0].SelectedItem.Text);
   }
}

if you have a doubt else, let me know.
 
Share this answer
 
Comments
Shahvez Irfan 14-Mar-12 7:56am    
lad, this would obviously work since the condition never meets! the selected item will always be null, and hence the condition will be skipped through! what i want is to go inside the if condition
elent 14-Mar-12 9:52am    
oh,i see.you can specify a option,like this :

RadioButtonList[] RBLPain = new RadioButtonList[2];
RBLPain[0] = new RadioButtonList();
RBLPain[0].Items.Add("Yes");
RBLPain[0].Items.Add("No");
RBLPain[0].Items[0].Selected = true;
Panel1.Controls.Add(RBLPain[0]);
if (RBLPain[0].SelectedItem.Text == "Yes")
{
Response.Write(RBLPain[0].SelectedItem.Text);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900