【问题标题】:"Object reference not set to an instance of an object" Error“对象引用未设置为对象的实例”错误
【发布时间】:2012-10-01 14:39:15
【问题描述】:

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

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

    }
}

【问题讨论】:

    标签: c# oop nullreferenceexception access-modifiers object-reference


    【解决方案1】:

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

    添加这样的构造函数可以解决问题。

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

    【讨论】:

    • 谢谢!希望我能投票支持一个以上的答案。由于您之前发布了答案并且有更多代码,我将提交答案。
    【解决方案2】:

    您是否在您的AccountManager 中创建Accounts

    你应该在某个地方做:

    Accounts = new List<Account>();
    

    编辑

    您有公开的set 访问器。你可以这样做:

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

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

    【讨论】:

    • 不,我没有。我会在物业中这样做吗?
    • 谢谢!固定的。我希望我能投票给一个正确的答案。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-09
      • 2012-08-31
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2012-09-19
      相关资源
      最近更新 更多