2013-02-21 72 views
-2

我的問題是,我試圖創建一個應用程序,並且據我所見,它應該是原始的,我根本找不到該錯誤。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; 
     } 
    } 
} 

如果你需要更多的代碼,請告訴我。

+0

他顯示錯誤發生的位置,並且還初始化按鈕事件處理程序中的currentAccount。 – Inisheer 2013-02-21 02:19:28

+2

向我們展示完整的例外情況,包括堆棧跟蹤等。 – 2013-02-21 02:20:28

+1

這將是學習如何更有效地使用調試器的絕佳機會。如果您使用的是Visual Studio,請使用「調試」 - >「例外」選項啓用任何CLR例外。現在,當拋出異常而不是未處理時,您的應用程序將會中斷 - 這將使您有機會查看所有應用程序狀態並真正瞭解發生了什麼。如果你不使用VS--大多數其他調試器(包括WinDbg,mdbg等)都支持這一點。 – 2013-02-21 02:36:37

回答

1

空引用異常總是意味着同樣的事情:你還沒有初始化一個變量。在這種情況下,您聲明CAccount currentAccount = null;作爲類成員。如果您需要它非空,則需要在調用DisplayBalance()之前的一段時間調用new CAccount()來初始化它。例如,如果用戶在單擊Button1之前單擊Button2,則您將null參考。同樣,如果myBank.GetAccount()返回null,你也將null參考。堆棧跟蹤將有助於縮小這些原因。

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); 
     } 
    } 

通過這樣做,你可以保證進入函數的值已被正確設置。

0

不是ToString一個函數嗎?它不應該是這樣的

txtBalance.Text = currentAccount.Balance.ToString(); 
相關問題