0

This application shows total females, males and total number of students from an XML document when the "Count" button is pressed. The problem is the program throws an error when the "Count" button is pressed and im not sure why?. Second, im not sure that all three totals are correct. I am very new to programming and would like some assistance if possible.

namespace DebugSession
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lblTotal;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label lblMales;
        private System.Windows.Forms.Label lbl;
        private System.Windows.Forms.Button btnCount;
        private System.Windows.Forms.Label lblFemale;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

      public Form1()
      {
         //
         // Required for Windows Form Designer support
         //
        InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
       }

         /// <summary>
         /// Clean up any resources being used.
         /// </summary>
      protected override void Dispose( bool disposing )
      {
        if( disposing )
        {
            if (components != null) 
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
      }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    private void label1_Click(object sender, System.EventArgs e)
    {


    }

    private void btnCount_Click(object sender, System.EventArgs e)
    {
        XmlDocument studentData = null;
        XmlNodeList studentList = null;
        TotalClass totalClass = null;
        string gender = "";

        studentData = new XmlDocument();
                studentData.LoadXml("<root>"
                    + "<STUDNT ID=\"7\" Gender=\"M\"></STUDNT>"
                    + "<STUDNT ID=\"16\" Gender=\"F\"></STUDNT>"
                    + "<STUDNT ID=\"22\" Gender=\"F\"></STUDNT>"
                    + "<STUDNT ID=\"25\" Gender=\"M\"></STUDNT>"
                    + "<STUDNT ID=\"27\" Gender=\"F\"></STUDNT>"
                    + "<STUDNT ID=\"32\" Gender=\"M\"></STUDNT>"
                    + "<STUDNT ID=\"35\" Gender=\"f\"></STUDNT>"
                    + "<STUDNT ID=\"45\" Gender=\"M\"></STUDNT>"
                    + "<STUDNT ID=\"4423453244\" Gender=\"F\"></STUDNT>"
                    + "<STUDNT ID=\"44344\" Gender=\"F\"></STUDNT>"
                    + "</root>");

        studentList = studentData.SelectNodes("//STUDENT");
        if(studentList != null && studentList.Count > 0)
        {
            foreach(XmlElement student in studentList)
            {
                gender = student.GetAttribute("Gender");
                switch(gender)
                {
                    case "F":
                        totalClass.Females++;
                        break;

                    default:
                    case "M":
                        totalClass.Males++;
                        break;
                }
            }// end loop
        }

        this.lblMales.Text = totalClass.Males.ToString();
        this.lblFemale.Text = totalClass.Females.ToString();
        this.lblTotal.Text = (totalClass.Females + totalClass.Males).ToString();


    }//btnCount_Click
}
}

I also have a TotalClass

using System;

namespace DebugSession
{
    /// <summary>
/// Summary description for TotalClass.
/// </summary>
public class TotalClass
 {
    /// <summary>
    /// Gets and Sets number of males
    /// </summary>
   public int Males
    {
        set{this.males = value;}
        get{return this.males;}
    }
    private int males = -1;

    /// <summary>
    /// Gets and Sets number of females
    /// </summary>
    public int Females
    {
        set{this.females = value;}
        get{return this.females;}
    }
    private int females = -1;
}//end class
}`
ryan g
  • 13
  • 4
  • 2
    share your error trace – SSH Jun 13 '15 at 22:07
  • its throwing An unhandled exception of type 'System.NullReferenceException' occurred in DebugSession.exe Additional information: Object reference not set to an instance of an object. – ryan g Jun 13 '15 at 22:13

2 Answers2

1

There are a lot of problems in your code:

First: you search for elements "STUDENT" but your XML contains "STUDNT". This causes your entire loop to be skipped and jump directly to the label text settings (and this causes the subsequent NullReferenceException)

Second: you cannot use an instance of a class without instancing it

 TotalClass totalClass = new TotalClass();

Third: You switch for uppercase "F" and "M" but one of your xml elements has a lowercase "f". This causes that entry to be counted as a Male because the default is coupled to the "M" case. Change your switch to

switch(gender.ToUpper())

Fourth: In TotalClass you initialize the counter for Female and Male to -1, of course this leads to an incorrect result, change it to

 private int males = 0;
 private int females = 0;
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Change:

TotalClass totalClass = null;

To:

TotalClass totalClass = new TotalClass();
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40