C# 未处理NullReferenceException(找不到错误源)
我的问题是,我正在尝试创建一个应用程序,据我所知,它应该是原始的,我根本找不到错误 下面是我的代码。我将评论错误发生的地方。 表格代码C# 未处理NullReferenceException(找不到错误源),c#,.net,winforms,nullreferenceexception,C#,.net,Winforms,Nullreferenceexception,我的问题是,我正在尝试创建一个应用程序,据我所知,它应该是原始的,我根本找不到错误 下面是我的代码。我将评论错误发生的地方。 表格代码 namespace TechBank { public partial class Tech_Bank : Form { CAccount currentAccount = null; CBank myBank = new CBank(); private void displayBalance
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;
}
}
}
如果您需要更多代码,请通知我。空引用异常总是意味着相同的事情:您没有初始化变量。在本例中,您声明
CAccount currentAccount=null代码>作为类成员。如果需要将其设置为非null,则需要在调用DisplayBalance()之前的某个时间通过调用new CAccount()
对其进行初始化。例如,如果用户在单击按钮1之前单击按钮2,您将返回null ref。同样,如果myBank.GetAccount()返回null,您也将返回null ref。堆栈跟踪将有助于缩小原因。因为您正在将“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);
}
}
通过这样做,您可以保证进入函数的值已被正确设置。是否正在串入函数?难道不是这样吗
txtBalance.Text = currentAccount.Balance.ToString();
他显示错误发生的位置,并在按钮事件处理程序中初始化currentAccount。向我们显示完整的异常,包括堆栈跟踪等。这将是学习如何更有效地使用调试器的绝佳机会。如果您使用的是Visual Studio,请使用“调试->异常”选项启用对任何CLR异常的中断。现在,当抛出异常而不是未处理异常时,您的应用程序将中断-这将使您有机会查看所有应用程序状态并真正了解发生了什么。如果您不使用VS-大多数其他调试器(包括WinDbg、mdbg等)都支持此功能