组合框对象 - C# 上的 NullReferenceException

本文关键字:上的 NullReferenceException 对象 组合 | 更新日期: 2023-09-27 18:30:59

我正在尝试将XML文件中的一些元素显示到使用选项卡的表单中。

但是,我在以下代码行上收到错误"NullReferenceException":

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;

此代码位于按钮单击中,单击后会生成新的组合框。我这样做的方式如下:

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);
        }

整数 i 是一个类变量,每次单击按钮时我都会递增它以创建新的组合框。

上面的代码片段可能会令人困惑,因为它们没有顺序,所以这里是整个类:

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
        }

下面是 XML:

<Course>
  <CourseName>BEng Software Engineering</CourseName>
  <Modules>
    <Module>
      <moduleCode>ECSE401</moduleCode>
      <moduleTitle>Programming Methodology</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Coursework</assessmentOne>
      <assessmentOneWeight>40</assessmentOneWeight>
      <assessmentTwo>Coursework</assessmentTwo>
      <assessmentTwoWeight>40</assessmentTwoWeight>
      <assessmentThree>Test</assessmentThree>
      <assessmentThreeWeight>20</assessmentThreeWeight>
    </Module>
    <Module>
      <moduleCode>ECSC404</moduleCode>
      <moduleTitle>Computer Systems Fundamentals</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Test1</assessmentOne>
      <assessmentOneWeight>30</assessmentOneWeight>
      <assessmentTwo>Test2</assessmentTwo>
      <assessmentTwoWeight>30</assessmentTwoWeight>
      <assessmentThree>Test3</assessmentThree>
      <assessmentThreeWeight>40</assessmentThreeWeight>
    </Module>
    <Module>
      <moduleCode>EBSY401</moduleCode>
      <moduleTitle>Information and Data Modelling</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Test</assessmentOne>
      <assessmentOneWeight>25</assessmentOneWeight>
      <assessmentTwo>Coursework1</assessmentTwo>
      <assessmentTwoWeight>10</assessmentTwoWeight>
      <assessmentThree>Coursework2</assessmentThree>
      <assessmentThreeWeight>35</assessmentThreeWeight>
      <assessmentFour>Coursework3</assessmentFour>
      <assessmentFourWeight>30</assessmentFourWeight> 
    </Module>
    <Module>
      <moduleCode>ECSC405</moduleCode>
      <moduleTitle>Software Development Principles</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Test1</assessmentOne>
      <assessmentOneWeight>30</assessmentOneWeight>
      <assessmentTwo>Coursework</assessmentTwo>
      <assessmentTwoWeight>40</assessmentTwoWeight>
      <assessmentThree>Test2</assessmentThree>
      <assessmentThreeWeight>30</assessmentThreeWeight>
    </Module>
    <Module>
      <moduleCode>ECSC407</moduleCode>
      <moduleTitle>Web Technology</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Tutorials</assessmentOne>
      <assessmentOneWeight>20</assessmentOneWeight>
      <assessmentTwo>Coursework</assessmentTwo>
      <assessmentTwoWeight>20</assessmentTwoWeight>
      <assessmentThree>Exam</assessmentThree>
      <assessmentThreeWeight>60</assessmentThreeWeight>
    </Module>
    <Module>
      <moduleCode>ECSC409</moduleCode>
      <moduleTitle>Software Engineering Principles</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Coursework1</assessmentOne>
      <assessmentOneWeight>40</assessmentOneWeight>
      <assessmentTwo>Coursework2</assessmentTwo>
      <assessmentTwoWeight>30</assessmentTwoWeight>
      <assessmentThree>Coursework3</assessmentThree>
      <assessmentThreeWeight>30</assessmentThreeWeight>
    </Module>
    <Module>
      <moduleCode>ECSC408</moduleCode>
      <moduleTitle>Mathematics for Computing</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Coursework</assessmentOne>
      <assessmentOneWeight>50</assessmentOneWeight>
      <assessmentTwo>Exam</assessmentTwo>
      <assessmentTwoWeight>50</assessmentTwoWeight>
    </Module>
    <Module>
      <moduleCode>EBSY400</moduleCode>
      <moduleTitle>Communication and Learning Skills</moduleTitle>
      <credits>15</credits>
      <assessmentOne>Coursework</assessmentOne>
      <assessmentOneWeight>30</assessmentOneWeight>
      <assessmentTwo>Coursework</assessmentTwo>
      <assessmentTwoWeight>70</assessmentTwoWeight>
    </Module>
  </Modules>
</Course>

我不明白为什么我会收到空引用异常,请有人帮忙

组合框对象 - C# 上的 NullReferenceException

索引

i 处的组合框可能未被选中(因此SelectedItem返回 null),或者d可能为 null

看到你使用上面的document.Descendants没有问题,我认为是选择给你带来了麻烦

尝试:

ComboBox cb = comboBoxModuleSelect[i] as ComboBox;
if (cb.SelectedItem != null ) {
  var assessment1 = from d in document.Descendants("moduleTitle") 
    where (d.Value == cb.SelectedItem.ToString())
    select d.Parent.Element("assessmentOne").Value;
}

我猜你已经向组合框添加了一堆项目,但没有选择它们。这就是为什么SelectedItem返回null.