0

I am having an issue adding a new Account() to a list that is in FileAccountRepository.cs, I am still learning about inheritance, so there might be something that I do not know. In PremiumAccountTestRepository.cs, I am trying to pass the _account that is a new Account() with properties and try to add it to the list that is in FileAccountRepository.cs. The error message I get is that _fileAccountRepository was null on PremiumAccountTestRepository.cs. Would like to know the reason I cannot add to the list that is FileAccountRepository.cs?

FileAccountRepository.cs

public class FileAccountRepository : IAccountRepository
{
    public List<Account> fileAccounts = new List<Account>();

    public FileAccountRepository(string mode)
    {
        GetUsers(mode);
    }        

    public void GetUsers(string mode) {            
        var dictionaryOfModes = new Dictionary<string, string>
        {
            ["FreeTest"] = "F",
            ["BasicTest"] = "B",
            ["PremiumTest"] = "P"
        };

        string path = @".\Accounts.txt";
        string[] rows = File.ReadAllLines(path);

        for (int i = 1; i < rows.Length; i++)
        {
            string[] columns = rows[i].Split(',');

            if (columns[3] == dictionaryOfModes[mode])
            {
                Account _account = new Account();
                _account.AccountNumber = columns[0];
                _account.Name = columns[1];
                _account.Balance = Decimal.Parse(columns[2]);
                if (columns[3] == "F")
                {
                    _account.Type = AccountType.Free;
                }
                else if (columns[3] == "B")
                {
                    _account.Type = AccountType.Basic;
                }
                else if (columns[3] == "P")
                {
                    _account.Type = AccountType.Premium;
                }

                //fileAccounts.Add(_account);
                StoreAccounts(_account);
            }
        }            
    }

    public Account LoadAccount(string AccountNumber)
    {
        return fileAccounts.FirstOrDefault(x => x.AccountNumber == AccountNumber);
    }

    public void SaveAccount(Account account)
    {
        //_account = account;
    }

    public void StoreAccounts(Account addAccount)
    {
        fileAccounts.Add(addAccount);
    }
}

PremiumAccountTestRepository.cs

public class PremiumAccountTestRepository : IAccountRepository
{
    private FileAccountRepository _fileAccountRepository;        

    public PremiumAccountTestRepository(FileAccountRepository fileAccountRepository)
    {
        _fileAccountRepository = fileAccountRepository;
    }

    public PremiumAccountTestRepository()
    {
        StoreAccounts(_account);
    }

    private static Account _account = new Account
    {
        Name = "Premium Account",
        Balance = 100M,
        AccountNumber = "44444",
        Type = AccountType.Premium
    };

    public Account LoadAccount(string AccountNumber)
    {

        if (_fileAccountRepository.fileAccounts.Any(x => x.AccountNumber == AccountNumber))
        {
            return _account;
        }

        return null;
    }

    public void SaveAccount(Account account)
    {
        _account = account;            
    } 

    public void StoreAccounts(Account addAccount)
    {
        _fileAccountRepository.fileAccounts.Add(addAccount); //_fileAccountRepository was null.
    }
}

AccountManagerFactory.cs

public static AccountManager Create()
    {
        string mode = ConfigurationManager.AppSettings["Mode"].ToString();

        switch (mode)
        {
            case "FreeTest":
                return new AccountManager(new FileAccountRepository(mode), new FreeAccountTestRepository());                    
            case "BasicTest":
                return new AccountManager(new FileAccountRepository(mode), new BasicAccountTestRepository());
            case "PremiumTest":
                return new AccountManager(new FileAccountRepository(mode), new PremiumAccountTestRepository());

            default:
                throw new Exception("Mode value in app config is not valid");
        }


    }
9
  • 2
    You don't show the code where you initialize an instance of the PremiumAccountTestRepository class, but the default constructor does not set _fileAccountRepository, and the other one accepts a null argument value. In either of those cases, you'd have this issue. Commented Dec 6, 2019 at 1:31
  • You may want to show the exception stack trace to help diagnose your issue. Commented Dec 6, 2019 at 1:31
  • 1
    Does this answer your question? What is a NullReferenceException, and how do I fix it? Commented Dec 6, 2019 at 1:41
  • Why you are using two constructors in PremiumAccountTestRepository? it looks like the dependency injection didn't initialize the FileAccountRepository, that's why it is null Commented Dec 6, 2019 at 1:44
  • I am calling PremiumAccountTestRepository, but not passing in anything. But I need to initialize fileAccountRepository. Commented Dec 6, 2019 at 1:47

3 Answers 3

1

Try this code, I was not able to test it :

  public class PremiumAccountTestRepository : IAccountRepository
    {
    private FileAccountRepository _fileAccountRepository;
    private Account _account;   

    public PremiumAccountTestRepository(FileAccountRepository fileAccountRepository)
    {
        _fileAccountRepository = fileAccountRepository;

        _account= new Account
        {
            Name = "Premium Account",
            Balance = 100M,
            AccountNumber = "44444",
            Type = AccountType.Premium
        };


        StoreAccounts(_account);
    }


    public Account LoadAccount(string AccountNumber)
    {

        if (_fileAccountRepository.fileAccounts.Any(x => x.AccountNumber == AccountNumber))
        {
            return _account;
        }

        return null;
    }

    public void SaveAccount(Account account)
    {
        _account = account;            
    } 

    public void StoreAccounts(Account addAccount)
    {
        _fileAccountRepository.fileAccounts.Add(addAccount); //_fileAccountRepository was 
                                                                  null.
    }
   }

The issue in your code is that you have two constructors, _fileAccountRepository is not initialized with the dependency injection because of the other empty constructor

Sign up to request clarification or add additional context in comments.

9 Comments

Ok I get it, so in my AccountMangerFactory.cs, what would I have to pass in to new PremiumAccountTestRepository() ?
you don't have to pass anything the dependency injector should take care of creating new instances, do you still got any errors?
it has an error on AccountManagerFactory.cs on new PremiumAccountTestRepository(), says that there is no argument given that corresponds to required formal paramer fileAccountRepository
because you are trying to initialize an object with dependency injection in a static class, it will not work, what kind of IoC container you are using to manage the dependency injection?
Got it working now, using the video I watched helped.
|
0

_fileAccountRepository is set in exactly one place in your code, in

public PremiumAccountTestRepository(FileAccountRepository fileAccountRepository)

The second creator

public PremiumAccountTestRepository()

Does not set it, so it will be null.

I suggest putting a break in both creators and see which is being called, and then you will know where from.

I don't see the top level driving code in your posting. I strongly suspect that the second form of the creator is getting called, and thus _fileAccountRepository is null.

1 Comment

I added the code for AccountManagerFactory.cs, which is where I am calling. As you can see, I have an issue. I don't know if I should be passing in something or not.
0

what happens if this line is altered from

private FileAccountRepository _fileAccountRepository

to

private FileAccountRepository _fileAccountRepository = new FileAccountRepository()

1 Comment

there is no argument that corresponds to the required formal parameter mode... (string mode)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.