2

我在弄清楚为什么我在表示层中收到“对象引用未设置为对象的实例”错误时遇到了问题:

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

            }

        }

    }
}

业务逻辑层

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

    }
}

业务对象

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

    }
}
4

3 回答 3

6

AccountManager从不初始化Accounts属性。因此TempAccountManager.Accounts为空。

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

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

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

}
于 2012-10-11T01:43:23.123 回答
3

Accounts在你的AccountManager?

你应该在某处做:

Accounts = new List<Account>();

编辑

你有公共set访问者。你可以这样做:

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

或者按照 Joel Mueller 的建议为类添加一个构造函数。但是仔细想想,如果你需要公众set。它提供了一个机会,可以将您的集合完全替换为对象。

于 2012-10-11T01:41:30.830 回答
1

不确定,但是您在哪里初始化帐户?

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

您的 get、set 接口提供访问权限,但未定义值。

于 2012-10-11T01:43:44.247 回答