NullReferenceException未处理(找不到错误的来源)

时间:2013-02-21 02:14:53

标签: c# .net winforms nullreferenceexception

我的问题是我正在尝试创建一个应用程序,据我所知,它应该是原始的,我根本找不到错误。

以下是我的代码。我将评论错误发生的位置。 表格代码

namespace TechBank
{
    public partial class Tech_Bank : Form
    {
        CAccount currentAccount = null;

        CBank myBank = new CBank();

        private void displayBalance()
        {
            if (lstAccounts.Items.Count != 0)
            {
                txtBalance.Text = currentAccount.Balance.ToString; //where the error hits
                txtCustomer.Text = currentAccount.CustomerName;
                txtAccountType.Text = Convert.ToString(currentAccount.AccountType);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Open_Account form = new Open_Account();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                currentAccount = new CAccount(typeAccount.checking, "", 4);
                if (form.rbtChequing.Checked)
                    currentAccount.AccountType = typeAccount.checking;
                if (form.rbtSavings.Checked)
                    currentAccount.AccountType = typeAccount.savings;

                try
                { 
                    currentAccount.Balance = Convert.ToDouble(form.txtStartingBalance.Text); 
                }
                catch (FormatException)
                { 
                    MessageBox.Show("Please enter valid information", "Error in account creation, please double check the values are correct"); 
                }

                currentAccount.CustomerName = form.txtCustomerName.Text;

                myBank.OpenAccount(currentAccount);
                lstAccounts.Items.Add(currentAccount.AccountID);
                currentAccount = myBank.GetAccount(lstAccounts.SelectedIndex);
                lstAccounts.SelectedIndex = lstAccounts.Items.Count - 1;
                displayBalance();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Transaction form = new Transaction(currentAccount);
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
            }
            displayBalance();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (lstAccounts.Items.Count != 0)
            {
                myBank.CloseAccount(currentAccount);
                lstAccounts.Items.RemoveAt(lstAccounts.SelectedIndex);
                txtAccountType.Clear();
                txtBalance.Clear();
                txtCustomer.Clear();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }


        private void lstAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentAccount = myBank.GetAccount(lstAccounts.SelectedIndex);
            displayBalance();
        }
    }
}

下面的CAccount.cs代码

namespace TechBank
{
    public enum typeAccount
    {
        checking,
        savings
    }

    public class CAccount
    {
        private static Random randomNumber = new Random();

        private typeAccount mAccountType;
        private double mBalance;
        private string mCustomer;
        private string mID;

        public CAccount(typeAccount newType, string newCustomer, double newBalance)
        {
            mAccountType = newType;
            mCustomer = newCustomer;
            mBalance = newBalance;
            mID = Convert.ToString(randomNumber.Next(1, 9999));
        }

        public typeAccount AccountType
        {
            get { return mAccountType; }
            set { mAccountType = value; }
        }

        public double Balance
        {
            get { return mBalance; }
            set { mBalance = value; }
        }

        public string CustomerName
        {
            get { return mCustomer; }
            set { mCustomer = value; }
        }

        public string AccountID
        {
            get { return mID; }
        }

        public void Deposit(double Amount)
        {
            if (IsPositiveNumber(Amount, 0))
                mBalance += Amount;
        }

        public bool IsPositiveNumber(double larger, double smaller)
        {
            return (larger >= smaller);
        }

        public void Withdraw(double Amount)
        {
            if (IsPositiveNumber(mBalance, Amount))
                mBalance -= Amount;
        }
    }
}

如果您需要更多代码,请通知我。

3 个答案:

答案 0 :(得分:1)

空引用异常总是意味着相同的事情:你没有初始化变量。在这种情况下,您将CAccount currentAccount = null;声明为类成员。如果您需要它为非null,则需要在调用DisplayBalance()之前调用new CAccount()来初始化它。例如,如果用户在单击Button1之前单击Button2,则将为null ref。同样,如果myBank.GetAccount()返回null,你也将为null ref。堆栈跟踪有助于缩小这些原因中的哪一个。

答案 1 :(得分:0)

因为您正在将'currentAccount'初始化为null,所以当您在'displayBalance'函数中访问它时,您无法保证它将被设置。我知道你可能认为它已被设置,因为你总是在调用'displayBalance()'之前设置它,但显然这不会发生在某个地方。

比为另一个要访问的函数设置类级变量更好的选择是将CAccount作为参数传递给'displayBalance()'方法,将其签名更改为

    private void displayBalance(CAccount account)
    {
        if (lstAccounts.Items.Count != 0)
        {
            txtBalance.Text = account.Balance.ToString;
            txtCustomer.Text = account.CustomerName;
            txtAccountType.Text = Convert.ToString(account.AccountType);
        }
    }

通过这样做,您可以保证正确设置进入函数的值。

答案 2 :(得分:0)

ToString是不是一个函数?不应该看起来像这样

txtBalance.Text = currentAccount.Balance.ToString();