简体   繁体   中英

Accessing a dynamic RadioButtonList array object

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

  static int jimmy = 0;


    protected void Button5_Click(object sender, EventArgs e)
    {
        int sizeOfPain = GlobalVariables.sympLCWR1Pain.Count;

        RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];

        Label1.Visible = false;
        RadioButtonList1.Visible = false;


        Label[] Labella = new Label[sizeOfPain];

        if (jimmy < sizeOfPain)
        {
            Labella[jimmy] = new Label();
            RBLPain[jimmy] = new RadioButtonList();

            Labella[jimmy].Text = GlobalVariables.sympLCWR1Pain[jimmy];

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

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

            Panel1.Controls.Add(Labella[jimmy]);
            Panel1.Controls.Add(RBLPain[jimmy]);



                if (RBLPain[jimmy].SelectedIndex == 0)
                {
                    GlobalVariables.sympLCWR1Yes.Add(GlobalVariables.sympLCWR1Pain[jimmy]);
                }


        }

        else
        {
            Label2.Text = "YOUS DONE!";

                Label3.Text = GlobalVariables.sympLCWR1Yes[0];

            Button5.Visible = false;

        }  

        jimmy++;


    }

i get the exception at the if condition. Any help would be appreciated thanks :)

What that error means is that you are trying to access something that hasn't yet been instantiated. In your updated code, I see you have the following within your click event handler:

 RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];
 Label[] Labella = new Label[sizeOfPain];

This means that every time the click event is handled, you are redeclaring the RBLPain and Labella arrays. Also, when execution leaves leaves the handler, the variables fall out of scope, so you will not be able to use them in other functions, or use the changes made within the handler from one call to the next. I don't know what the rest of your code is doing, but despite the seemingly unnecessary arrays, execution should survive your click event.

In your original post you were trying to access the SelectedItem.Text property of the RBLPain[jimmy] . In this revision you are checking the SelectedIndex instead. When SelectedIndex is -1, SelectedItem will be null, perhaps this led to your original problem. Regardless of what is changed on your form, because you are creating a new RadioButtonList during every click event, you are not working with the values from your form - SelectedIndex will always be -1 from what I can see.

I dont understand why you checking the condition .If you are creating rbl on buttonclick first item should always get selected. Anyway use RBLPain[jimmy].SelectedIndex=0; before if condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM