"对象引用未设置为对象的实例"错误

Jon*_*n H 2 c# oop access-modifiers object-reference nullreferenceexception

我有问题弄清楚为什么我接收"未设置为对象实例的对象引用"此表示层中的错误:

TempAccountManager.Accounts.Add(tempAccount);

我已经使用Visual Studios调试器和帐户获取创建了代码.我相信我有一个访问模块的问题,不确定.

表达层

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);

            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

业务逻辑层

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; }

    }
}
Run Code Online (Sandbox Code Playgroud)

业务对象

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;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*ler 6

AccountManager班从来没有初始化的Accounts属性.因此TempAccountManager.Accounts是空的.

添加像这样的构造函数将修复它.

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

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

}
Run Code Online (Sandbox Code Playgroud)