0

I'm trying to display some elements from my XML file to my form which is using tabs.

However I'm getting the error "NullReferenceException" on the following line of 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;

This code is inside a button click which generates new combo boxes once clicked. The way I did that is as follows:

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

integer i is a class variable, which I increment every time the button is clicked in order to create a new combo box.

The above snippets might be confusing because they're not in order, so here is the whole class:

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

        }

and here is the 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>

I don't understand why I'm getting a nullreference exception, please could someone help

2
  • On a side note, don't do this: (comboBoxModuleSelect[i] as ComboBox).Location. If you are assuming that the safe cast won't return null then there is no reason to perform a safe cast to begin with, just use a C style cast (and for that matter, perform one cast and store the reference in a variable). Also, there is no reason to use ArrayList here (there almost never is since the introduction of generics in 1.1). Your code is making a lot of assumptions and apparently at least one of them is wrong. Commented Apr 11, 2012 at 19:13
  • Actually I think the problem is in my logic, the whole operation is in a button click. When I run the program there are no combo boxes on the screen, so the operation compare d.Value with comboBox.SelectedItem is always going to be null because I haven't selected anything, hence the program closes in the first click. Am I correct in assuming this? Commented Apr 11, 2012 at 19:28

2 Answers 2

2

The combobox at index i might not be selected (so SelectedItem returns null), or d might be null

Seeing that you use document.Descendants above with no problem, I think it is the selection that causing you the trouble

Try:

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

ya, the thing is that once I click the button, it pops up the error, I don't get to enter any value in the combo box
Select an element before clicking the button?
that solves the error problem, but I don't get any value outputted onto my label, I think there's something wrong with getting the XML elements
You could output the values of item in assessment1 for each (incremental) part of the from/where/select construct and see it agrees with what you would expect
0

I guess you've added a bunch of items to combobox but made none of them selected. That's why SelectedItem returns null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.