title: NullReferenceException on combo box object - C#
body: <p>I'm trying to display some elements from my XML file to my form which is using tabs.</p>
  
  <p>However I'm getting the error "NullReferenceException" on the following line of code:</p>
  
  <pre><code>var assessment1 = from d in document.Descendants("moduleTitle")
                              where d.Value == (comboBoxModuleSelect[i] as ComboBox).SelectedItem.ToString() //error here on this line
                              select d.Parent.Element("assessmentOne").Value;
  </code></pre>
  
  <p>This code is inside a button click which generates new combo boxes once clicked. The way I did that is as follows:</p>
  
  <pre><code>private void buttonLevel4Add_Click(object sender, EventArgs e)
          {
              var document = XDocument.Load(workingDir + @"\Level4.xml");
              comboBoxModuleSelect.Add(new ComboBox());
              System.Drawing.Point p = new System.Drawing.Point(7, 57 + i * 25);
              (comboBoxModuleSelect[i] as ComboBox).Location = p;
              (comboBoxModuleSelect[i] as ComboBox).Size = new System.Drawing.Size(183, 20);
              tabPageLevel4.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
          }
  </code></pre>
  
  <p>integer i is a class variable, which I increment every time the button is clicked in order to create a new combo box.</p>
  
  <p>The above snippets might be confusing because they're not in order, so here is the whole class:</p>
  
  <pre><code>public partial class Form1 : Form
      {
          String workingDir = Directory.GetCurrentDirectory();
          ArrayList comboBoxModuleSelect = new ArrayList();
          ArrayList labelAssessments = new ArrayList();
          int i = 0; //for combo boxes
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              tabControl.SelectTab(1);
          }
  
          private void buttonLevel4Add_Click(object sender, EventArgs e)
          {
              var document = XDocument.Load(workingDir + @"\Level4.xml");
              comboBoxModuleSelect.Add(new ComboBox());
              System.Drawing.Point p = new System.Drawing.Point(7, 57 + i * 25);
              (comboBoxModuleSelect[i] as ComboBox).Location = p;
              (comboBoxModuleSelect[i] as ComboBox).Size = new System.Drawing.Size(183, 20);
              tabPageLevel4.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
  
              this.labelAssessments.Add(new Label());
              System.Drawing.Point pLabel = new System.Drawing.Point(200 + i * 25, 81);
              (labelAssessments[i] as Label).Location = pLabel;
              (labelAssessments[i] as Label).Size = new System.Drawing.Size(80, 13);
              tabPageLevel4.Controls.Add(labelAssessments[i] as Label);
  
  
              var moduleName = from d in document.Descendants("moduleTitle")
                          select d.Value;
              foreach (var item in moduleName)
              {
                  //add the module names into the dropdown for students to select
                  (comboBoxModuleSelect[i] as ComboBox).Items.Add(item);
              }
  
              var assessment1 = from d in document.Descendants("moduleTitle")
                              where d.Value == (comboBoxModuleSelect[i] as ComboBox).SelectedItem.ToString()
                              select d.Parent.Element("assessmentOne").Value;
              foreach (var item in assessment1)
              {
  
                  (labelAssessments[i] as Label).Text = item.ToString();
  
              }
  
  
              i++;//for drawing
  
          }
  </code></pre>
  
  <p>and here is the XML:</p>
  
  <pre><code>&lt;Course&gt;
    &lt;CourseName&gt;BEng Software Engineering&lt;/CourseName&gt;
    &lt;Modules&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSE401&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Programming Methodology&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Coursework&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;40&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;40&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Test&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;20&lt;/assessmentThreeWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSC404&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Computer Systems Fundamentals&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Test1&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;30&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Test2&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;30&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Test3&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;40&lt;/assessmentThreeWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;EBSY401&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Information and Data Modelling&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Test&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;25&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework1&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;10&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Coursework2&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;35&lt;/assessmentThreeWeight&gt;
        &lt;assessmentFour&gt;Coursework3&lt;/assessmentFour&gt;
        &lt;assessmentFourWeight&gt;30&lt;/assessmentFourWeight&gt; 
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSC405&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Software Development Principles&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Test1&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;30&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;40&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Test2&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;30&lt;/assessmentThreeWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSC407&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Web Technology&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Tutorials&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;20&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;20&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Exam&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;60&lt;/assessmentThreeWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSC409&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Software Engineering Principles&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Coursework1&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;40&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework2&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;30&lt;/assessmentTwoWeight&gt;
        &lt;assessmentThree&gt;Coursework3&lt;/assessmentThree&gt;
        &lt;assessmentThreeWeight&gt;30&lt;/assessmentThreeWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;ECSC408&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Mathematics for Computing&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Coursework&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;50&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Exam&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;50&lt;/assessmentTwoWeight&gt;
      &lt;/Module&gt;
      &lt;Module&gt;
        &lt;moduleCode&gt;EBSY400&lt;/moduleCode&gt;
        &lt;moduleTitle&gt;Communication and Learning Skills&lt;/moduleTitle&gt;
        &lt;credits&gt;15&lt;/credits&gt;
        &lt;assessmentOne&gt;Coursework&lt;/assessmentOne&gt;
        &lt;assessmentOneWeight&gt;30&lt;/assessmentOneWeight&gt;
        &lt;assessmentTwo&gt;Coursework&lt;/assessmentTwo&gt;
        &lt;assessmentTwoWeight&gt;70&lt;/assessmentTwoWeight&gt;
      &lt;/Module&gt;
    &lt;/Modules&gt;
  &lt;/Course&gt;
  </code></pre>
  
  <p>I don't understand why I'm getting a nullreference exception, please could someone help</p>