title: Windows form application crashes unexpectedly C#
body: <p>Here is my code. I dont think there is anything wrong with the code. Sometimes it runs fine but sometimes it crashes unexpectedly.</p>
  
  <pre><code>namespace searchingConsoleWFA
  {
  public partial class Form1 : Form
  {
      searchingConsole.Class1 objj = new searchingConsole.Class1();
      List&lt;searchingConsole.Class1&gt; obj = new List&lt;searchingConsole.Class1&gt;();
  
      private void button3_Click(object sender, EventArgs e)
      {         
          String compName, tit, conName, phone, fax, addr, pCode, city, reg, cntry;
  
          if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)
               || String.IsNullOrEmpty(textBox3.Text) || String.IsNullOrEmpty(textBox4.Text)
               || String.IsNullOrEmpty(textBox5.Text) || String.IsNullOrEmpty(textBox6.Text)
               || String.IsNullOrEmpty(textBox7.Text) || String.IsNullOrEmpty(textBox8.Text)
               || String.IsNullOrEmpty(textBox9.Text) || String.IsNullOrEmpty(textBox10.Text)
              )
          {
              MessageBox.Show("Please Enter Complete Information", "Invalid");
          }
          else
          {
              compName = textBox1.Text;
              tit = textBox2.Text;
              conName = textBox3.Text;
              phone = textBox4.Text;
              fax = textBox5.Text;
              addr = textBox6.Text;
              pCode = textBox7.Text;
              city = textBox8.Text;
              reg = textBox9.Text;
              cntry = textBox10.Text;
  
              obj.Add(new searchingConsole.Class1(compName, tit, conName, phone, fax, addr,
                  pCode, city, reg, cntry));
              MessageBox.Show("Company Added!", "Registered");
          }
          textBox1.Clear();
          textBox2.Clear();
          textBox3.Clear();
          textBox4.Clear();
          textBox5.Clear();
          textBox6.Clear();
          textBox7.Clear();
          textBox8.Clear();
          textBox9.Clear();
          textBox10.Clear();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          String compName, conName;
          compName = textBox11.Text;
          conName = textBox12.Text;
  
          if (String.IsNullOrEmpty(textBox11.Text) &amp;&amp; String.IsNullOrEmpty(textBox12.Text))
          {
              MessageBox.Show("Enter Company Name and Contact Name", "Invalid");
          }
          else if (String.IsNullOrEmpty(textBox11.Text))
          {
              MessageBox.Show("Enter Company Name", "Invalid");
          }
          else if (String.IsNullOrEmpty(textBox12.Text))
          {
              MessageBox.Show("Enter Contact Name", "Invalid");
          }
          else
          {
              for (int i = 0; i &lt; obj.Count; i++)
              {
                  if (obj[i].getCompanyName() == compName &amp;&amp; obj[i].getContactName() == conName)
                  {
                      MessageBox.Show("Match Found", "Search Result");
                  }
                  else
                  {
                      MessageBox.Show("Match Not Found", "Search Result");
                  }
  
                  textBox1.Clear();
                  textBox2.Clear();
              }
          }
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo);
          if (result == DialogResult.Yes)
          {
              Environment.Exit(0);
          }
          else if (result == DialogResult.No)
          {
  
          }
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ehMkM.jpg" alt=" "></p>
  
  <p>This is the form I have created. On clicking 'Cancel' the program should exit. The 'OK' button is for searching and the 'Apply' button is for recording the data in some data structure.</p>
  
  <p>The problem is, if I create a record first and then exit then the program exits fine, but if I exit first it crashes. Similarly if some record is searched the program runs fine, but if text fields are left empty then program crashes.</p>
  
  <p>Any Help?</p>