2

Im Having Problems Figuring Out Why I am Receiving an "Object reference not set to an instance of an object" Error in the Presentation Layer with this line:

TempAccountManager.Accounts.Add(tempAccount);

I Have Walked Through the Code With Visual Studios Debugger and the Account Gets Created. I Believe I Have an Issue With an Access Modifer, Not Sure.

Presentation Layer

using myBudget.BusinessObject;
using myBudget.BusinessLogic;

namespace myBudget
{
    public partial class NewBudgetWizard : Form
    {

        public int CurrentStep { get; set; }

        public Account TempAccount = new Account();
        public AccountManager TempAccountManager = new AccountManager();

        public NewBudgetWizard()
        {

        private void createAccountList(ListView lvAccounts)
        {

            foreach (ListViewItem lvi in lvAccounts.Items)
            {

                int tempAccNumber = Int32.Parse(lvi.SubItems[0].Text);
                string tempAccName = lvi.SubItems[1].Text;
                string tempAccType = lvi.SubItems[2].Text;
                decimal tempAccBalance = decimal.Parse(lvi.SubItems[3].Text, System.Globalization.NumberStyles.Currency);

                Account tempAccount = new Account(tempAccNumber, tempAccName, tempAccType, tempAccBalance, DateTime.Now);
                TempAccount = new Account(tempAccNumber, tempAccName, tempAccType, tempAccBalance, DateTime.Now);

                TempAccountManager.Accounts.Add(tempAccount);

            }

        }

    }
}

Business Logic Layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using myBudget.BusinessObject;

namespace myBudget.BusinessLogic
{
    public class AccountManager : Account
    {

        public List<Account> Accounts { get; set; }

    }
}

Business Object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myBudget.BusinessObject
{
    public class Account
    {

        public int AccountID { get; set; }

        public int UserID { get; set; }

        public int Number { get; set; }

        public string Name { get; set; }

        public string Type { get; set; }

        public decimal Balance { get; set; }

        public DateTime ReconcileTimeStamp { get; set; }

        public Account()
        {

        }

        public Account(int number, string name, string type, decimal balance, DateTime reconcileTimeStamp)
        {
            Number = number;
            Name = name;
            Type = type;
            Balance = balance;
            ReconcileTimeStamp = reconcileTimeStamp;
        }

    }
}
Jon H
  • 273
  • 2
  • 5
  • 14

3 Answers3

6

The AccountManager Class Never Initializes The Accounts Property. Therefore TempAccountManager.Accounts Is Null.

Adding A Constructor Like This Will Fix It.

public class AccountManager : Account
{
    public AccountManager()
    {
        Accounts = new List<Account>();
    }

    public List<Account> Accounts { get; set; }

}
Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
  • 1
    Thank You! Wish I could Vote for More then one Answer. Since You Posted the Answer Before and Have More Code I am Going to Submit as Answered. – Jon H Oct 11 '12 at 01:53
3

Do you create Accounts in your AccountManager?

You should do somewhere:

Accounts = new List<Account>();

EDIT

You've got public set accessor. You may do:

TempAccountManager.Accounts = new List<Account>();

Or add a constructor to the class, as Joel Mueller suggested. But think over, if you need the public set. It gives an opportunity to completely replace your collection out of the object.

horgh
  • 17,918
  • 22
  • 68
  • 123
1

Not sure, but where do you initialize Accounts?

public List<Account> Accounts { get; set; }

Your get, set interface provides access, but doesn't define the value.

Kirk B.
  • 456
  • 2
  • 6