我试图从我的XML文件显示一些元素到我的表单使用选项卡。组合框对象上的NullReferenceException - C#
但是我得到以下行的代码错误“的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>
我不明白为什么我得到空引用异常,请有人帮忙
请注意,不要这样做:'(comboBoxModuleSelect [i]作为组合框).Location'。如果您假设安全演员阵容不会返回null,那么就没有理由先执行安全演员,只需使用C风格演员阵容(就此而言,执行一次演员并将参考存储在变量)。另外,没有理由在这里使用'ArrayList'(自从1.1中引入泛型以来几乎没有)。你的代码做了很多假设,显然至少有一个是错误的。 – 2012-04-11 19:13:28
其实我觉得问题出在我的逻辑上,整个操作都在按钮上点击。当我运行程序时,屏幕上没有组合框,所以操作比较d.Value和comboBox.SelectedItem总是会为空,因为我没有选择任何东西,因此程序在第一次点击时关闭。 我是否正确地承认这一点? – a7omiton 2012-04-11 19:28:43