我创建了一个名为的类Customer
,它有大约四个字段。我正确地掌握了所有吸气剂和吸气剂。
我从实例化了一个对象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。
我相信您的问题在这里:
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
为null。当您尝试在其上调用equals时,它将引发空引用。我建议更改if(!(customerName.Equals("")))
为if(!string.IsNullOrEmpty(customerName))
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句