ASP.net中出现Null引用异常错误

本文关键字:引用 异常 错误 Null net ASP | 更新日期: 2023-09-27 18:27:37

我必须将应用程序从windows窗体移植到ASP.net

在我尝试使用另一个表单将销售数据添加到表单之前,一切都很好。在windows表单中,它工作得很好,但当我在ASP中尝试时,我会得到"用户代码未处理空引用异常"

当我试图从我的主表单访问"添加新销售"时,这会弹出。

我的"添加销售表格"中的代码是:

public partial class AddSaleForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public Sale GetData()
    {
        return new Sale(IdTextbox.Text, DateTextbox.Text, LocationTextBox.Text, Convert.ToDouble(PitchCostTextbox.Text), Convert.ToInt32(NumPitchesTextbox.Text), CharityCheckbox.Checked, CharityNameTextbox.Text, CateringCheckBox.Checked);
    }

知道哪里出了问题吗?我在哪里可以修复它?

这是构造函数:

    namespace AntiqueSale
{
    [Serializable]
    public class Sale : IComparable<Sale>
    {
        private String saleId;
        public String SaleId
        {
          get { return saleId; }
          set { saleId = value; }
        }
        private String saleDate;
        private String location;
        private double pitchCost;
        private int numPitches;
        private bool charity;
        public bool Charity
        {
            get { return charity; }
            set { charity = value; }
        }
        private String charityName;
        private bool catering;
        public Sale(String saleId,
                        String saleDate,
                        String location,
                        double pitchCost,
                        int numPitches,
                        bool charity,
                        String charityName,
                        bool catering)
        {
            this.saleId = saleId;
            this.saleDate = saleDate;
            this.location = location;
            this.pitchCost = pitchCost;
            this.numPitches = numPitches;
            this.charity = charity;
            this.charityName = charityName;
            this.catering = catering;
        }
        public override string ToString()
        {
            String str;
            String charityString;
            String cateringString;
            String charityNameString;
            if (charity)
            {
                charityString = "Yes";
                charityNameString = charityName;
            }
            else
            {
                charityString = "No";
                charityNameString = "N/A";
            }
            if (catering)
            {
                cateringString = "Yes";
            }
            else
            {
                cateringString = "No";
            }
            str = String.Format("{0}: {1}: {2}: {3}: {4}: {5}: {6}: {7}",
                                    saleId,
                                    saleDate,
                                    location,
                                    pitchCost,
                                    numPitches,
                                    charityString,
                                    charityNameString,
                                    cateringString);
            return str;
        }
        public int CompareTo(Sale sale)
        {
            return this.SaleId.CompareTo(sale.SaleId);
        }
    }

构造函数不是我设计的,这是我收到的需要修改为asp的起始代码。

ASP.net中出现Null引用异常错误

您的问题是以下两行代码之一:

  1. 通过以下操作验证PitchCostTextbox.TextConvert.ToDouble(PitchCostTextbox.Text)行中不为空:

    double PitchCost = 0.0;
    if(!String.IsNullOrEmpty(PitchCostTextbox.Text))
    {
        PitchCost = Convert.ToDouble(PitchCostTextbox.Text);
    }
    
  2. 验证Convert.ToInt32(NumPitchesTextbox.Text)行中的NumPitchesTextbox.Text不为null或为空。

    int NumPitches = 0;
    if(!String.IsNullOrEmpty(NumPitchesTextbox.Text))
    {
        NumPitches = Convert.ToInt32(NumPitchesTextbox.Text);
    }