2

문제점이 있음 "개체 참조가 개체의 인스턴스로 설정되지 않았습니다"라는 오류가 발생하는 이유이 줄이 포함 된 프레젠테이션 계층의 오류 :"개체 참조가 개체의 인스턴스로 설정되지 않았습니다"오류

TempAccountManager.Accounts.Add (tempAccount);

Visual Studio 디버거를 사용하여 코드를 살펴보고 계정을 만들었습니다. 나는 액세스 변조자가 문제가 있다고 믿는다.

프리젠 테이션 레이어

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

    } 
} 

답변

6

AccountManager 클래스는 Accounts 속성을 초기화하지 않습니다. 따라서 TempAccountManager.Accounts은 Null입니다.

생성자를 추가하면 수정 될 것입니다.

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

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

} 
+1

감사합니다! 나는 하나 이상의 답변에 투표 할 수 있기를 소원합니다. 전에 답변을 게시하고 더 많은 코드가 있으니 답변을 제출하십시오. –

3

D o AccountManagerAccounts을 만드시겠습니까?

당신은 어딘가에 수행해야합니다

Accounts = new List<Account>(); 

편집

당신은 공공 set 접근을 가지고있다.

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

또는 조엘 뮬러가 제안, 클래스에 생성자를 추가 : 당신은 할 수 있습니다. 그러나 대중이 필요하면 이상 생각하십시오 set. 그것은 개체에서 컬렉션을 완전히 대체 할 수있는 기회를 제공합니다.

+0

아니요. 그 재산에서 내가 그걸 할까? –

+0

감사합니다! 결정된. 나는 하나의 정답에 투표 할 수 있기를 바란다. –

+0

@ 존 H 귀하의 의견에 가장 적합한 답변을 하나만 수락하실 수 있습니다. – horgh

1

확실하지 않지만 계정을 어디에서 초기화합니까?

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

get, set 인터페이스는 액세스를 제공하지만 값은 정의하지 않습니다.

관련 문제