引发异常的简单表单应用程序
本文关键字:表单 应用程序 简单 异常 | 更新日期: 2023-09-27 17:55:54
当按下"计数"按钮时,此应用程序显示XML文档中的女性,男性和学生总数。 问题是程序按下"计数"按钮时引发错误,我不确定为什么?其次,我不确定这三个总数是否正确。我对编程很陌生,如果可能的话,我希望得到一些帮助。
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
}
}
我还有一个总类
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
}`
你的代码中有很多问题:
首先:您搜索元素"STUDENT",但您的XML包含"STUDNT"。这会导致跳过整个循环并直接跳转到标签文本设置(这会导致后续NullReferenceException
)
第二:你不能在不实例化的情况下使用类的实例
TotalClass totalClass = new TotalClass();
第三:您切换为大写"F"和"M",但其中一个 xml 元素具有小写"f"。这会导致该条目计为男性,因为默认值与"M"大小写耦合。将交换机更改为
switch(gender.ToUpper())
第四:TotalClass
将女性和男性的计数器初始化为 -1,当然这会导致不正确的结果,将其更改为
private int males = 0;
private int females = 0;
更改:
TotalClass totalClass = null;
自:
TotalClass totalClass = new TotalClass();