创建Object时尝​​试并捕获运行时问题

时间:2014-09-15 01:41:08

标签: c#

我创建了一个名为Customer的类,它有大约四个字段。我有所有的getter和setter正确。

我从welcomeForm实例化了一个对象。

当我点击Save按钮时,我会运行:

 private void saveBtn_Click(object sender, EventArgs e)
    {
        Customer newCustomer = new Customer();
        try
        {
            if (isValidData())
            {
                newCustomer.CustomerName = customerNameTxtBox.Text;
                newCustomer.CustomerID = Convert.ToInt64(customerIdTxtBox.Text);

                newCustomer.CustomerCheckInAmmount = Convert.ToDouble(checkAmountTxtBox.Text);

                double feeTotal = (newCustomer.CustomerCheckInAmmount * (newCustomer.CheckFeeAmmount));

                newCustomer.CustomerCheckOutAmmount = newCustomer.CustomerCheckInAmmount - feeTotal;

                feeTotal = (feeTotal + (newCustomer.CustomerCheckOutAmmount - (int)newCustomer.CustomerCheckOutAmmount));

                totalFeeTxtBox.Text = feeTotal.ToString("c");
                checkTotalTxtBox.Text = newCustomer.CustomerCheckOutAmmount.ToString("c");
            }
        }
        catch (NullReferenceException ex)
        {
            MessageBox.Show("Processor Usage" + ex.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\n\n" +
                ex.GetType().ToString() + "\n" +
                ex.StackTrace, " Exceptions");
        }
    }

    public bool isValidData()
    {
        return
            IsPresent(customerNameTxtBox, "Customer Name") &&
            IsDouble(checkAmountTxtBox, " Check In") &&
            IsPresent(customerIdTxtBox, "Customer Phone");
    }

    public bool IsPresent(TextBox textBox, string name)
    {
        if (textBox.Text == "")
        {
            MessageBox.Show(name + " is a required field.", "Entry Error");
            textBox.Focus();
            return false;
        }
        return true;
    }

    public bool IsDouble(TextBox textBox, string name)
    {
        try
        {
            Convert.ToDouble(textBox.Text);
            return true;

        }
        catch (FormatException)
        {
            MessageBox.Show(name + " Must be decimal value.", "Entry Error.");
            textBox.Focus();
            return false;
        }
    }

以下是Customer

 class Customer
{
    private string customerName;
    private Int64 customerID;
    private double customerCheckInAmmount;
    private const double checkFeeAmmount= 3/100;
    private double customerCheckOutAmmount;

    public Customer() { }

    public Customer(string cName, Int64 cID, double checkInCustomer,double checkOutCustomer)
    {
        this.customerName = cName;
        this.customerID = cID;
        this.customerCheckInAmmount = checkInCustomer;
        this.customerCheckOutAmmount = checkOutCustomer;
    }

    public string CustomerName
    {
        get
        {
            return customerName;
        }
        set
        {
            if(!(customerName.Equals("")))
            {
                customerName = value;
            }
        }
    }

    public Int64 CustomerID
    {
        get
        {
            return customerID;
        }
        set
        {
            if(!(customerID <= 0))
            {
                customerID = value;
            }
        }
    }

    public double CustomerCheckInAmmount
    {
        get
        {
            return CustomerCheckInAmmount;
        }
        set
        {
            if(!(customerCheckInAmmount <=0.0))
            {
                customerCheckInAmmount = value;
            }
        }

    }

    public double CustomerCheckOutAmmount
    {
        get
        {
            return CustomerCheckOutAmmount;
        }
        set
        {
            if (!(customerCheckOutAmmount <= 0.0))
            {
                customerCheckOutAmmount = value;
            }
        }
    }

    public double CheckFeeAmmount
    {
        get
        {
            return CheckFeeAmmount;
        }

    }
    public string getDisplayString(string sep)
    {
        return (customerName +
            sep + customerID +
            sep + customerCheckInAmmount +
            sep + checkFeeAmmount +
            sep + customerCheckOutAmmount);
    }
}

}

当我建立时,我没有遇到任何问题,但是当我跑步时,我确实遇到了问题。我收到此错误

  

Processor UsageObject引用未设置为Object的实例。

**在我在学校的项目中,我以相同的方式创建了一个对象并且没有出错。

任何建议都会很好。我正在使用Visual Studio 2013 Ultimate。

1 个答案:

答案 0 :(得分:2)

我相信你的问题在这里:

public string CustomerName
{
    get
    {
        return customerName;
    }
    set
    {
        if(!(customerName.Equals("")))
        {
            customerName = value;
        }
    }
}

由于您正在调用Customer newCustomer = new Customer();,因此您未初始化customerName字符串。因此,当newCustomer.CustomerName = customerNameTxtBox.Text;被调用时,它会调用if(!(customerName.Equals("")))。此时,您还没有为customerName提供值,customerName为空。当你尝试在它上面调用equals时,它会抛出一个空引用。我建议将if(!(customerName.Equals("")))更改为if(!string.IsNullOrEmpty(customerName))