简单的表单应用程序抛出异常

时间:2015-06-13 22:06:01

标签: c# winforms c#-4.0

此应用程序显示了当" Count"时,来自XML文档的总女性,男性和学生总数。按下按钮。问题是该计划 " Count"按下按钮,我不知道为什么?其次,我不确定所有三个总数是否正确。我对编程非常陌生,如果可能的话,我希望得到一些帮助。

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

我也有一个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
}`

2 个答案:

答案 0 :(得分:1)

您的代码中存在很多问题:

首先:您搜索元素&#34;学生&#34;但是您的XML包含&#34; STUDNT&#34;。这会导致您的整个循环被跳过并直接跳转到标签文本设置(这会导致后续NullReferenceException

第二:你不能在没有实例化的情况下使用类的实例

 TotalClass totalClass = new TotalClass();

第三:您切换为大写&#34; F&#34;和&#34; M&#34;但是你的一个xml元素有一个小写&#34; f&#34;。这导致该条目被计为男性,因为默认值与“&#34; M&#34;案件。将您的开关更改为

switch(gender.ToUpper())

第四:TotalClass中,您将女性和男性的计数器初始化为-1,当然这会导致错误的结果,将其更改为

 private int males = 0;
 private int females = 0;

答案 1 :(得分:0)

变化:

TotalClass totalClass = null;

要:

TotalClass totalClass = new TotalClass();