c# - simple form application throwing an exception -


this question has answer here:

this application shows total females, males , total number of students xml document when "count" button pressed. problem program throws error when "count" button pressed , im not sure why?. second, im not sure 3 totals correct. new programming , assistance if possible.

namespace debugsession {     /// <summary>     /// summary description 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 windows form designer support          //         initializecomponent();           //          // todo: add constructor code after initializecomponent call          //        }           /// <summary>          /// clean resources being used.          /// </summary>       protected override void dispose( bool disposing )       {         if( disposing )         {             if (components != null)              {                 components.dispose();             }         }         base.dispose( disposing );       }      /// <summary>     /// main entry point 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 have totalclass

using system;  namespace debugsession {     /// <summary> /// summary description totalclass. /// </summary> public class totalclass  {     /// <summary>     /// gets , sets number of males     /// </summary>    public int males     {         set{this.males = value;}         get{return this.males;}     }     private int males = -1;      /// <summary>     /// gets , sets number of females     /// </summary>     public int females     {         set{this.females = value;}         get{return this.females;}     }     private int females = -1; }//end class }` 

there lot of problems in code:

first: search elements "student" xml contains "studnt". causes entire loop skipped , jump directly label text settings (and causes subsequent nullreferenceexception)

second: cannot use instance of class without instancing

 totalclass totalclass = new totalclass(); 

third: switch uppercase "f" , "m" 1 of xml elements has lowercase "f". causes entry counted male because default coupled "m" case. change switch

switch(gender.toupper()) 

fourth: in totalclass initialize counter female , male -1, of course leads incorrect result, change

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

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -