Im Having Problems Figuring Out Why I am Receiving an "Object reference not set to an instance of an object" Error in the Presentation Layer with this line:
TempAccountManager.Accounts.Add(tempAccount);
I Have Walked Through the Code With Visual Studios Debugger and the Account Gets Created. I Believe I Have an Issue With an Access Modifer, Not Sure.
Presentation Layer
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);
}
}
}
}
Business Logic Layer
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; }
}
}
Business Object
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;
}
}
}
The AccountManager Class Never Initializes The Accounts Property. Therefore TempAccountManager.Accounts Is Null.
Adding A Constructor Like This Will Fix It.
public class AccountManager : Account
{
public AccountManager()
{
Accounts = new List<Account>();
}
public List<Account> Accounts { get; set; }
}
Do you create Accounts in your AccountManager?
You should do somewhere:
Accounts = new List<Account>();
EDIT
You've got public set accessor. You may do:
TempAccountManager.Accounts = new List<Account>();
Or add a constructor to the class, as Joel Mueller suggested. But think over, if you need the public set. It gives an opportunity to completely replace your collection out of the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With