I am trying to read AccountNumber from a text file, but it does not seem to be reading the AccountNumber from the text file, and I can confirm that the text file location string is correct.
(我正在尝试从文本文件中读取AccountNumber,但似乎没有从文本文件中读取AccountNumber,因此我可以确认文本文件位置字符串正确。)
Also, note that currently the user is only set to access BasicAccountTestRepository class, but I am trying to get the LoadAccounts method to be able to call GetAccounts() from FileAccountRepository.cs. (另外,请注意,当前仅将用户设置为访问BasicAccountTestRepository类,但是我正在尝试获取LoadAccounts方法,以便能够从FileAccountRepository.cs调用GetAccounts()。)
According to my instructions, I should be able to read from the text file in this class. (根据我的指示,我应该能够从此类中的文本文件中进行读取。)
I am getting a NullReferenceException on LoadAccounts method, says "Object reference not set to an instance of an object." (我在LoadAccounts方法上收到NullReferenceException,说“对象引用未设置为对象的实例”。)
Accounts.txt
(Accounts.txt)
AccountNumber,Name,Balance,Type
10001,Free Account,100,F
20002,Basic Account,500,B
30003,Premium Account,1000,P
FileAccountRepository.cs
(FileAccountRepository.cs)
public class FileAccountRepository : IAccountRepository
{
List<Account> accounts = new List<Account>();
public Account _account = new Account();
public void GetUsers() {
string path = @".Accounts.txt";
string[] rows = File.ReadAllLines(path);
for (int i = 1; i < rows.Length; i++)
{
string[] columns = rows[i].Split(',');
//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;
}
accounts.Add(_account);
}
}
public Account LoadAccount(string AccountNumber)
{
if (accounts.Any(x => x.AccountNumber == AccountNumber))
{
return _account;
}
return null;
}
public void SaveAccount(Account account)
{
_account = account;
}
}
AccountManager.cs
(AccountManager.cs)
...
public AccountLookupResponse LookupAccount(string accountNumber)
{
AccountLookupResponse response = new AccountLookupResponse();
FileAccountRepository fileAccount = new FileAccountRepository(); //NullReferenceException
fileAccount.GetUsers();
response.Account = _accountRepository.LoadAccount(accountNumber);
if(response.Account == null)
{
response.Success = false;
response.Message = $"{accountNumber} is not a valid account.";
}
else
{
response.Success = true;
}
return response;
}
...
ask by Alejandro H translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…