C# &引用;对象引用未设置为对象的实例;错误
我很难弄清楚为什么我在表示层中收到“对象引用未设置为对象实例”错误,这行代码是: TempAccountManager.Accounts.Add(tempAccount)强> 我已经使用Visual Studio调试器浏览了代码,并创建了帐户。我相信我有一个访问修改器的问题,不确定 表示层C# &引用;对象引用未设置为对象的实例;错误,c#,oop,nullreferenceexception,access-modifiers,object-reference,C#,Oop,Nullreferenceexception,Access Modifiers,Object Reference,我很难弄清楚为什么我在表示层中收到“对象引用未设置为对象实例”错误,这行代码是: TempAccountManager.Accounts.Add(tempAccount) 我已经使用Visual Studio调试器浏览了代码,并创建了帐户。我相信我有一个访问修改器的问题,不确定 表示层 using myBudget.BusinessObject; using myBudget.BusinessLogic; namespace myBudget { public partial clas
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 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; }
}
}
您是否在
AccountManager
中创建账户
你应该在某个地方做:
Accounts = new List<Account>();
或者像Joel Mueller建议的那样,向类添加构造函数。但是仔细想想,如果你需要公共
设置
。它提供了完全替换对象之外的集合的机会。AccountManager类从不初始化Accounts
属性。因此,TempAccountManager.Accounts
为空
添加这样的构造函数可以修复它
public class AccountManager : Account
{
public AccountManager()
{
Accounts = new List<Account>();
}
public List<Account> Accounts { get; set; }
}
公共类AccountManager:帐户
{
公共会计经理()
{
账户=新列表();
}
公共列表帐户{get;set;}
}
不确定,但在哪里初始化帐户
public List<Account> Accounts { get; set; }
公共列表帐户{get;set;}
您的get,set接口提供了访问权限,但没有定义值。不,我没有。我会在酒店里这样做吗?谢谢!固定的。我希望我能投票给一个正确的答案。谢谢!但愿我能投票支持不止一个答案。因为你之前发布了答案,并且有更多的代码,所以我将作为答案提交。