我的問題是,我試圖創建一個應用程序,並且據我所見,它應該是原始的,我根本找不到該錯誤。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;
}
}
}
如果你需要更多的代碼,請告訴我。
他顯示錯誤發生的位置,並且還初始化按鈕事件處理程序中的currentAccount。 – Inisheer 2013-02-21 02:19:28
向我們展示完整的例外情況,包括堆棧跟蹤等。 – 2013-02-21 02:20:28
這將是學習如何更有效地使用調試器的絕佳機會。如果您使用的是Visual Studio,請使用「調試」 - >「例外」選項啓用任何CLR例外。現在,當拋出異常而不是未處理時,您的應用程序將會中斷 - 這將使您有機會查看所有應用程序狀態並真正瞭解發生了什麼。如果你不使用VS--大多數其他調試器(包括WinDbg,mdbg等)都支持這一點。 – 2013-02-21 02:36:37