在Visual Studio 2010中完成C#作业时遇到问题

本文关键字:作业 遇到 问题 Visual Studio 2010 | 更新日期: 2023-09-27 18:34:08

好的,所以我一直在尝试获得此任务中所有员工的总数、工人数量、总工资和平均工资的摘要。但是,当我在程序运行时单击程序中的"摘要按钮"时,我不断收到错误消息。"EX0406 中发生了类型为'System.NullReferenceException'的未处理异常.exe其他信息:对象引用未设置为对象的实例。

这是我收到的错误消息。我不太确定该怎么做。

看到我卡住了,我的代码不完整,但我希望得到有关如何显示摘要的任何帮助。我是编程新手。 如果我需要更具体,请告诉我。

这是我所拥有的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace EX0406
{
    public partial class PieceworkForm : Form
    {    //class variables 
        private string name;
        private int pieces;
        private string Average_Pay_Per_PERSON;
        private decimal total_Pay; 

        private List<MyData> MyList = new List<MyData>();
        public PieceworkForm()
        {
            InitializeComponent();
        }
        private void calculateButton_Click(object sender, EventArgs e)
        {
            //does the calculations 
            string name = (EmployeeNameTextBox).Text;
            int pieces = Convert.ToInt32(NumOfPiecesText.Text);
            if (name == "")
               MessageBox.Show("You must enter a name.");
            else if (pieces < 1 || pieces > 799)
               MessageBox.Show("Enter a number between 1 and 799.");
            else
            {
               MyData data = new MyData(name, pieces);
               MyList.Add(data);
               MessageBox.Show(string.Format("Amount Earned: {0:C}", data.Paid));
            }
        } 
        private void summaryButton_Click(object sender, EventArgs e)
        {       
            //Display info in msg box
            MyData data = new MyData(name, pieces);
            MyList.Add(data);
            try
            {
                string summaryString = ""
                                     + pieces.ToString()
                                     + "'n'n" + "Total pay:"
                                     + total_Pay.ToString("C")
                                     + "'n'n" + "Average pay per person:"
                                     + Average_Pay_Per_PERSON.ToString();
                MessageBox.Show(summaryString, "Summary:");
            }
            finally
            { 
                MessageBox.Show(SummaryOutPut.Text);  
            }
        }
        private void clearButton_Click(object sender, EventArgs e)
        {   //clears the employee and number of pieces text box
            EmployeeNameTextBox.Clear();
            NumOfPiecesText.Clear();
        }
        private void clearAllButton_Click(object sender, EventArgs e)
        {
            EmployeeNameTextBox.Clear();
            NumOfPiecesText.Clear();
            SummaryOutPut.Clear(); 
        }
        private void exitButton_Click(object sender, EventArgs e)
        {   //closes the program when user clicks exit
            this.Close(); 
        }
        private void nameTextBox_TextChanged(object sender, EventArgs e)
        {
        }

        public class MyData
        {
            public MyData(string name, int pieces)
            {
                //if else statements 
                Name = name;
                Pieces = pieces;
                if (Pieces < 200) //if pieces are less than 200, multiply by .50
                    Paid = Pieces * .50;
                else if (Pieces < 400)  //mulitply by .55 if less than 400
                    Paid = Pieces * .55;
                else if (Pieces < 600) // multiply by .60 if less than 600
                    Paid = Pieces * .60;
                else  // multiply by .65 if everything else doesnt apply 
                    Paid = Pieces * .65;
            }
            public string Name;
            public int Pieces;
            public double Paid;
        }
        private void SummaryOutPut_TextChanged(object sender, EventArgs e)
        {
        }
        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
        private void helpToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
        }
        /* the following lines of code are menu items*/
        private void aboutToolStripMenuItem_Click_1(object sender, EventArgs e)
        {   //MSG box that displays name of lab and my name when clicking about and help item.
            MessageBox.Show("Lab 5 by J Soto"); 
        }
        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {    //exit menu item that closes the windows form. 
            this.Close(); 
        }
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {    //menu item that clears the employee name and # of pieces text box.
            EmployeeNameTextBox.Clear();
            NumOfPiecesText.Clear();
        }
        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {    //menu item to clear all the text boxes.
            EmployeeNameTextBox.Clear();
            NumOfPiecesText.Clear();
            SummaryOutPut.Clear(); 
        }
        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
        private void colorToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
        private void calculatePayToolStripMenuItem_Click(object sender, EventArgs e)
        {  //does the calculations 
            string name = (EmployeeNameTextBox).Text;
            int pieces = Convert.ToInt32(NumOfPiecesText.Text);
            if (name == "")
                MessageBox.Show("You must enter a name.");
            else if (pieces < 1 || pieces > 799)
                MessageBox.Show("Enter a number between 1 and 799.");
            else
            {
                MyData data = new MyData(name, pieces);
                MyList.Add(data);
                MessageBox.Show(string.Format("Amount Earned: {0:C}", data.Paid));
            }
        } 
        private void summaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    }
}

在Visual Studio 2010中完成C#作业时遇到问题

啊,我记得不理解空引用异常的日子。与其直接给你答案,不如告诉你如何诊断问题。

单击调用summaryButton_Click的按钮时会出现异常。您知道错误发生在该代码块中。空引用意味着您的一个变量为空(没有为其分配值)。

要查找该值,请在summaryButton_Click函数内的每一行上放置断点,请运行程序并单击按钮。然后,您将能够看到哪一行代码引发错误。我想你会发现它是名称变量,而碎片变量尚未被赋值。

成使用 this 关键字引用类级变量的习惯。它让正在阅读您的代码的其他程序员知道该变量在方法范围内的某个地方不存在。

至于您的空引用异常,当您尝试在 summaryButton_Click() 中引用其值时,pieces尚未初始化。您可能会感到困惑,因为所有名称都与您的局部和类级变量发生冲突。

现在对于其他一些错误,我可以看到:

total_Pay也没有在 summaryButton_Click() 中初始化。请记住,当您尝试引用不存在的内存时,获得空引用异常。

此外,由于某种原因,您在另一个类中嵌套了几个类。通过查看您发布的代码,我可以告诉您没有理由这样做。花一些时间清理代码。您会发现可读代码更容易理解。