我正在尝试将 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>
我不明白为什么我得到一个空引用异常,请有人帮忙