林有問題搞清楚爲什麼我收到一個「對象引用不設置到對象的實例」錯誤與該行表示層:「對象引用不設置到對象的實例」錯誤
TempAccountManager.Accounts.Add(tempAccount);
我已經通過Visual Studios Debugger和帳戶獲取創建了代碼。我相信我有一個訪問修改器的問題,不確定。
表示層
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;
}
}
}
謝謝!希望我可以投票多一個答案。自從你發佈答案之前,並有更多的代碼,我要提交爲答案。 –