Here is my C# code for Mainform.cs
public partial class MainForm : Form
{
private Customer cust;
public MainForm()
{
InitializeComponent();
}
private void buttonDeposit_Click(object sender, EventArgs e)
{
// I believe this is where I am going wrong... but I cannot understand why...
DepositDialog dlg = new DepositDialog(cust.Accounts);
dlg.ShowDialog();
}
private void buttonWithdraw_Click(object sender, EventArgs e)
{
// Here it is again...
WithdrawDialog dlg = new WithdrawDialog(cust.Accounts);
dlg.ShowDialog();
}
}
Here is the code for Customer.cs:
class Customer
{
private BankAccountCollection accounts;
private TransactionCollection transactionHistory;
public Customer()
{
accounts.Add(new SavingsAccount(true,200));
accounts.Add(new SavingsAccount(true, 1000));
accounts.Add(new LineOfCreditAccount(true, 0));
}
public BankAccountCollection Accounts
{
get { return accounts; }
}
public TransactionCollection TransactionHistory
{
get { return transactionHistory; }
}
}
When I try to run the program, I am getting a JIT error that tells me that the object reference is not set to an instance of an object. How do I initialize the field:
private Customer cust;
and why does it need to be initialized?:
The problem is that in the constructor you are using the accounts object without creating the object first using the ‘new’ keyword. You have to initialize the objects as below before using the .Add method.