对象数组作为参数传递,要求对象实例化?

时间:2019-02-10 22:25:09

标签: c# object

我正在使用以下方法将名为“客户”的对象传递给方法。我先前将customerArray实例化为main中的空对象。

主要

Customer[] customerArray = new Customer[] { };

传递的数组

customerArray = Methods.NewAccount(customerArray);

我遇到的问题在方法内部。我没有收到语法错误,但收到错误代码,说明Object reference not set to an instance of an object

方法

public static Customer[] NewAccount(Customer [] createdCustomers)
{
    if (createdCustomers == null)
    {
        Console.Clear();
        Console.WriteLine("You must first enter a new customer. Press any key to continue...");
        Console.ReadKey();
        Methods.NewCustomer();
    }
    else
    {
        for (int i = 0; i < createdCustomers.Length; i++)
        {
            Console.Clear();
            Console.WriteLine($"Please enter the Account Balance for {createdCustomers[i].CustomerName}.");
            string accountBalanceStr = Console.ReadLine();
            decimal accountBalanceDec;
            while (!decimal.TryParse(accountBalanceStr, out accountBalanceDec))
            {
                Console.Clear();
                Console.WriteLine($"INVALID RESPONSE\n\r\n\r" +
                    $"Please enter the Account Balance for {createdCustomers[i].CustomerName}.");
                accountBalanceStr = Console.ReadLine();
            }
            createdCustomers[i].CustomerAccount.AccountBalance = accountBalanceDec;

            Console.Clear();
            Console.WriteLine($"Please enter the Account Number for {createdCustomers[i].CustomerName}.");
            string accountNumberStr = Console.ReadLine();
            int accountNumberInt;
            while (!int.TryParse(accountNumberStr, out accountNumberInt))
            {
                Console.Clear();
                Console.WriteLine($"INVALID RESPONSE\n\r\n\r" +
                    $"Please enter the Account Number for {createdCustomers[i].CustomerName}.");
                accountBalanceStr = Console.ReadLine();
            }
            createdCustomers[i].CustomerAccount.AccountNumber = accountNumberInt;
        }

    }

    return createdCustomers;
}

我正在尝试更改对象本身内的值,并将其传递回Main。每当我输入我的信息时,该错误就会立即消失。我不完全知道发生了什么。

我相信我的问题在于如何处理数据。我还尝试实例化一个新对象,但这对我没有任何帮助,因为我需要来自先前创建的对象的所有数据。

在线错误

createdCustomers[i].CustomerAccount.AccountBalance = accountBalanceDec

客户对象

class Customer
    {
        public string CustomerName { get; set; }

        public CheckingAccount CustomerAccount { get; set; }

        public Customer(string _customerName, CheckingAccount _customerAccount)
        {
            CustomerName = _customerName;
            CustomerAccount = _customerAccount;
        }
    }

检查帐户对象

class CheckingAccount
    {
        public decimal AccountBalance {get; set;}
        public int AccountNumber { get; set; }

        public CheckingAccount(decimal _accountBalance, int _accountNumber)
        {
            AccountBalance = _accountBalance;
            AccountNumber = _accountNumber;
        }
    }

创建的客户

public static Customer [] NewCustomer()
        {
            Console.Clear();
            Console.WriteLine("How many customers will you be creating? ");
            string createdCustomersStr = Console.ReadLine();
            int createdCustomersInt;
            while(!Int32.TryParse(createdCustomersStr, out createdCustomersInt))
            {
                Console.Clear();
                Console.WriteLine("That is an invalid number, please enter the number of customers to create.");
                createdCustomersStr = Console.ReadLine();
            }
            Customer[] createdCustomer = new Customer [createdCustomersInt];
            for (int i = 0; i < createdCustomersInt; i++)
            {
                Console.Clear();
                Console.WriteLine($"Please input customer #{i+1}'s name. ");
                string createdNameStr = Console.ReadLine();
                Customer createdCustomerTemp = new Customer(createdNameStr, null);
                createdCustomer[i] = createdCustomerTemp;
            }
            return createdCustomer;

我查看的某些页面没有帮助:

What is a NullReferenceException, and how do I fix it?

What does "Object reference not set to an instance of an object" mean?

这些页面仅简单地说明了导致错误的原因,但仅以低级方式提供。我正在使用数组,以及传递这些数组的方法。我无法理解方法处理数组的方式以及如何实例化对象。问题不知道是什么导致了错误,而是“识别”比简单的ref错误更复杂的事情。

2 个答案:

答案 0 :(得分:1)

createdCustomers[i].CustomerAccount,其中CustomerAccount为空。

createdCustomers[i].CustomerAccount.AccountBalance可分为

var customerAccount = createdCustomers[i].CustomerAccount; //where customerAccount is set to null
customerAccount.AccountBalance //null.AccountBalance

因此NullReferenceException

答案 1 :(得分:0)

建议对

如果要更改值并返回相同的数组,首先需要将数组传递为ref/out。其次,需要在Customer对象中初始化CheckingAccount

所以

createdCustomers[i].CustomerAccount = new CheckingAccount(..);
C#中的

引用https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref