简体   繁体   中英

check if linq operation is null before execution C#

I have a Win-form with 60 Radio-buttons. I want to get the Text from the checked radio-buttons with this code:

private void button1_Click_1(object sender, EventArgs e)
    {

         string[] boxes = new string[30];
         string[] names = new string[30];

        for (int i = 1; i < boxes.Length; i++)
        {
            var label = this.Controls.Find("lb" + i, true)[0];

            var panelcontr = this.Controls.Find("panel" + i, true)[0] as Panel;
            var panels = panelcontr;

            var p = panels.Controls.OfType<RadioButton>()
                        .FirstOrDefault(r => r.Checked).Text;

            boxes[i] += p;
            names[i] += label.Text;
            tobeWritten += names[i] + boxes[i] + ",";
                textBox1.Text = "Anamnese(" + tobeWritten + ")";

        }
    }

It works totally fine but the problem is, when just one radio-box is unchecked I get a warning during Debug. I know why the Warning appears but I want the Program not to stop.

What I'm asking is: Is it possible to create a MessageBox which appears, when I haven't checked a radioButton which eg says "You have to assign every button". I click the "OK"-Button and I am able to check the unchecked Button. I tried with the suggestions from an other question at StackOverflow but no success because the function panels.Controls.OfType<RadioButton>() gets executed before the query.

You get a NullReferenceException here if there is no checked RadioButton :

var p = panels.Controls.OfType<RadioButton>()
    .FirstOrDefault(r => r.Checked).Text;

Because FirstOrDefault returns null since RadioButton is a reference type. Then you can't access it's Text property. So how to avoid that?

Store the result and check if it's null before you use it:

RadioButton firstCheckedRadioButton = panels.Controls.OfType<RadioButton>()
    .FirstOrDefault(r => r.Checked);
if(firstCheckedRadioButton != null)
{
    string text = firstCheckedRadioButton.Text;
    // ...
}

Would it help to replace

var p = panels.Controls.OfType<RadioButton>()
                        .FirstOrDefault(r => r.Checked).Text;

with

var ch = panels.Controls.OfType<RadioButton>()
                        .FirstOrDefault(r => r.Checked);
if (ch == null)
{
  // show message box and break;

}
var p = ch.Text;

Your issue is here:

var p = panels.Controls.OfType<RadioButton>()
                        .FirstOrDefault(r => r.Checked).Text;

If the result of the FirstOrDefault call is null, the Text call will throw a null reference exception. You can avoid this by splitting up the calls, so that you call FirstOrDefault, check whether the result is null, and then call Text only once you know the result is not null.

It works now with this code:

private void button1_Click_1(object sender, EventArgs e)
    {

         string[] boxes = new string[30];
         string[] names = new string[30];

        for (int i = 1; i < boxes.Length; i++)
        {

            var label = this.Controls.Find("lb" + i, true)[0];

            var panelcontr = this.Controls.Find("panel" + i, true)[0] as Panel;
            var panels = panelcontr;

            var radiobutton = panels.Controls.OfType<RadioButton>()
                        .FirstOrDefault(r => r.Checked);
            if(radiobutton==null)
            {
                MessageBox.Show("Check all Buttons!");
                break;
            }

            boxes[i] += radiobutton.Text;
            names[i] += label.Text;
            tobeWritten += names[i] + boxes[i] + ",";
            textBox1.Text = "Anamnese(" + tobeWritten + ")";

        }
    }

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