创建对象时尝试并捕获运行时问题

iso

我创建了一个名为的类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。

OxCantEven

我相信您的问题在这里:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

创建基类对象时的运行时多态

来自分类Dev

VBA运行时错误1004:尝试在Excel 2013中创建表时,对象_Global的方法范围失败

来自分类Dev

尝试访问在VBA(Visio)中在运行时创建的控件的属性时发生运行时错误

来自分类Dev

在运行时创建Command对象

来自分类Dev

在运行时创建对象?

来自分类Dev

发送回运行时创建的对象

来自分类Dev

尝试从Access创建MS Word文档文件时出现运行时错误5981

来自分类Dev

尝试创建ActiveX组件时出现运行时错误429

来自分类Dev

尝试从Access创建MS Word文档文件时出现运行时错误5981

来自分类Dev

在创建新对象时使用“尝试并捕获”

来自分类Dev

捕获运行时异常?

来自分类Dev

Delphi-在运行时创建引用对象时遇到麻烦

来自分类Dev

创建类型为System.Drawing.Point的运行时对象时出错

来自分类Dev

未知大小的数组作为类成员,用于在运行时创建数组对象(对象创建时)

来自分类Dev

尝试打印Unicode字符时出现运行时异常

来自分类Dev

尝试使用pip时出现运行时错误

来自分类Dev

在运行时通过 Java 创建 XML 文件的问题

来自分类Dev

尝试在Android中创建倒数计时器时出现运行时异常和空指针异常

来自分类Dev

尝试以编程方式创建用户窗体按钮时,运行时“ 13”错误类型不匹配

来自分类Dev

尝试从新创建的工作簿中删除工作表时出现运行时错误 - Outlook VBA

来自分类Dev

检查使用运行时对象创建调用的模拟

来自分类Dev

在运行时创建DEEP不可变对象

来自分类Dev

如何设计基于运行时模式的依赖对象创建?

来自分类Dev

OpenGLES-在运行时创建对象

来自分类Dev

在运行时过程性地创建和命名对象

来自分类Dev

Java:对象创建导致运行时错误

来自分类Dev

Android-使用Dagger在运行时创建对象

来自分类Dev

在运行时创建对象并使用它们

来自分类Dev

ActiveX组件无法创建对象VBA运行时错误

Related 相关文章

  1. 1

    创建基类对象时的运行时多态

  2. 2

    VBA运行时错误1004:尝试在Excel 2013中创建表时,对象_Global的方法范围失败

  3. 3

    尝试访问在VBA(Visio)中在运行时创建的控件的属性时发生运行时错误

  4. 4

    在运行时创建Command对象

  5. 5

    在运行时创建对象?

  6. 6

    发送回运行时创建的对象

  7. 7

    尝试从Access创建MS Word文档文件时出现运行时错误5981

  8. 8

    尝试创建ActiveX组件时出现运行时错误429

  9. 9

    尝试从Access创建MS Word文档文件时出现运行时错误5981

  10. 10

    在创建新对象时使用“尝试并捕获”

  11. 11

    捕获运行时异常?

  12. 12

    Delphi-在运行时创建引用对象时遇到麻烦

  13. 13

    创建类型为System.Drawing.Point的运行时对象时出错

  14. 14

    未知大小的数组作为类成员,用于在运行时创建数组对象(对象创建时)

  15. 15

    尝试打印Unicode字符时出现运行时异常

  16. 16

    尝试使用pip时出现运行时错误

  17. 17

    在运行时通过 Java 创建 XML 文件的问题

  18. 18

    尝试在Android中创建倒数计时器时出现运行时异常和空指针异常

  19. 19

    尝试以编程方式创建用户窗体按钮时,运行时“ 13”错误类型不匹配

  20. 20

    尝试从新创建的工作簿中删除工作表时出现运行时错误 - Outlook VBA

  21. 21

    检查使用运行时对象创建调用的模拟

  22. 22

    在运行时创建DEEP不可变对象

  23. 23

    如何设计基于运行时模式的依赖对象创建?

  24. 24

    OpenGLES-在运行时创建对象

  25. 25

    在运行时过程性地创建和命名对象

  26. 26

    Java:对象创建导致运行时错误

  27. 27

    Android-使用Dagger在运行时创建对象

  28. 28

    在运行时创建对象并使用它们

  29. 29

    ActiveX组件无法创建对象VBA运行时错误

热门标签

归档