lorenzoid
lorenzoid

Reputation: 1832

Initializing a field for an entire winform to use... (Object reference not set to instance of an object)

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?:

Upvotes: 0

Views: 496

Answers (7)

masterchris_99
masterchris_99

Reputation: 2733

public MainForm()
{
    InitializeComponent();
    cust = new Customer();
}

Upvotes: 0

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

It's not so much initialised as created.

 private Customer cust; // tells the compiler cust will be a Customer

 cust = new Customer(); // tells the compiler to create(and initialise) a Customer and point cust at it.

Until you do that cust will be null, and anyattemt to do somethingh customersih with it, will be an error.

Another way to think about what your variable cust is would be.

  cust = FindCustomerInCountry("France");

if you've no french customer cust will point to "nothing" i.e null.

keep going you'll get there.

Upvotes: 1

James Jenkins
James Jenkins

Reputation: 186

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.

private BankAccountCollection accounts = new BankAccountCollection();
private TransactionCollection transactionHistory = new TransactionCollection();

public Customer()
{
    accounts.Add(new SavingsAccount(true,200));
    accounts.Add(new SavingsAccount(true, 1000));
    accounts.Add(new LineOfCreditAccount(true, 0));
}

Upvotes: 2

NaveenBhat
NaveenBhat

Reputation: 3328

why does it need to be initialized?

Because, you cannot access non-static members without creating the instance.

You need to instantiate the class Customer using new keyword before accessing its instance members.

public partial class MainForm : Form
{
    private Customer cust;

    public MainForm()
    {
        InitializeComponent();
        cust = new Customer();
     }

Upvotes: 1

Brandon Moore
Brandon Moore

Reputation: 8790

"Customer cust;" only tells the compiler what type of variable cust can hold. It doesn't actually allocate any memory for a customer object... that's what the 'new' keyword does. Don't think of cust as an object so much as a pointer to a customer object that you can at any point change to point at a different customer object if you want.

Upvotes: 1

Donatas K.
Donatas K.

Reputation: 866

your cust is null.

initialize it:

private Customer cust = new Customer();

or

before this line

DepositDialog dlg = new DepositDialog(cust.Accounts);

Upvotes: 1

Alexander Galkin
Alexander Galkin

Reputation: 12554

You can either initialize it in your form constructor

public MainForm()
{
    InitializeComponent();
    cust = new Customer();
    // here eventually some intialization code...

or directly by declaration.

private Customer cust = new Customer() 
      {Accounts = new BankAccount[] 
          { new SavingsAccount(true,200), new SavingsAccount(true,200), 
            new SavingsAccount(true,200)} };

Upvotes: 1

Related Questions