Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.net中出现空引用异常错误_C#_Asp.net_Nullreferenceexception - Fatal编程技术网

C# ASP.net中出现空引用异常错误

C# ASP.net中出现空引用异常错误,c#,asp.net,nullreferenceexception,C#,Asp.net,Nullreferenceexception,我必须将应用程序从windows窗体移植到ASP.net 一切正常,直到我尝试使用另一个表单将销售数据添加到表单。在windows窗体中,它可以正常工作,但当我在ASP中尝试时,我得到了“用户代码未处理空引用异常” 当我尝试从主窗体访问“添加新销售来源”时,会弹出此对话框 我的“添加销售表单”中的代码是: 知道哪里出了问题吗?我在哪里可以解决 以下是构造函数: namespace AntiqueSale { [Serializable] public class Sale

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

一切正常,直到我尝试使用另一个表单将销售数据添加到表单。在windows窗体中,它可以正常工作,但当我在ASP中尝试时,我得到了“用户代码未处理空引用异常”

当我尝试从主窗体访问“添加新销售来源”时,会弹出此对话框

我的“添加销售表单”中的代码是:

知道哪里出了问题吗?我在哪里可以解决

以下是构造函数:

    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);
        }
    }
namespace AntiqueSale
{
[可序列化]
公共类销售:IComparable
{
私有字符串saleId;
公共字符串SaleId
{
获取{return saleId;}
设置{saleId=value;}
}
私人字符串日期;
私有字符串位置;
私人双重成本;
私人国际货币单位;
私人慈善机构;
公益慈善
{
获取{return charity;}
设置{charity=value;}
}
私有字符串charityName;
私人餐饮;
公开销售(字符串saleId,
字符串saleDate,
字符串位置,
双重成本,
int numPitches,
布尔慈善组织,
字符串charityName,
布勒(餐饮)
{
this.saleId=saleId;
this.saleDate=saleDate;
这个位置=位置;
this.pitchCost=pitchCost;
this.numPitches=numPitches;
这个。慈善=慈善;
this.charityName=charityName;
这就是餐饮;
}
公共重写字符串ToString()
{
字符串str;
字符串慈善字符串;
串串串;
字符串charityNameString;
国际单项体育联合会(慈善)
{
charityString=“是”;
charityNameString=charityName;
}
其他的
{
charityString=“否”;
charityNameString=“不适用”;
}
国际单项体育联合会(餐饮)
{
cateringString=“是”;
}
其他的
{
cateringString=“否”;
}
str=String.Format(“{0}:{1}:{2}:{3}:{4}:{5}:{6}:{7}”,
萨利德,
销售日期,
位置,
成本,
numPitches,
慈善字符串,
慈善名称字符串,
悬链线);
返回str;
}
公共国际比较(销售)
{
将此.SaleId.CompareTo(sale.SaleId)返回给;
}
}

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

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

  • 改为执行以下操作,以验证
    PitchCostTextbox.Text
    Convert.ToDouble(PitchCostTextbox.Text)
    中的
    PitchCostTextbox.Text
    是否为空:

    double PitchCost = 0.0;
    if(!String.IsNullOrEmpty(PitchCostTextbox.Text))
    {
        PitchCost = Convert.ToDouble(PitchCostTextbox.Text);
    }
    
  • 验证
    NumPitchesTextbox.Text
    Convert.ToInt32(NumPitchesTextbox.Text)
    中的
    NumPitchesTextbox.Text>是否为空


  • 几乎所有
    NullReferenceException
    的情况都是相同的。请参阅“”以获取一些提示。如果
    GetData()中的一行
    方法是引发错误的方法,然后该行中的一个对象是
    null
    。在调试器中,在该行上放置一个断点并检查对象。查看哪些对象是/是
    null
    。另外,您的“当我尝试从主窗体访问窗体时"听起来有点令人不安。你到底是如何做到这一点的?在WinForms中,你可以实例化新的表单并直接使用它们。在WebForms中不是这样的。虽然从技术上讲你可以创建页面对象,但你不应该需要也不想这样做。另一方面,请注意,接受如此多参数的构造函数是一个明确的sign的设计非常糟糕。重构它会让你的生活更轻松。如果你是作者,你也会从基础编程课程中受益。@Andrei你确定页面中的所有代码都是这样吗?假设页面标记声明了控件,你发布的代码不会引发NullReference异常,但会引发其他异常我将删除我的答案,直到你发布完整的代码和标记。
    double PitchCost = 0.0;
    if(!String.IsNullOrEmpty(PitchCostTextbox.Text))
    {
        PitchCost = Convert.ToDouble(PitchCostTextbox.Text);
    }
    
    int NumPitches = 0;
    if(!String.IsNullOrEmpty(NumPitchesTextbox.Text))
    {
        NumPitches = Convert.ToInt32(NumPitchesTextbox.Text);
    }