对象数组传递作为参数,请求对象的实例? [重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象数组传递作为参数,请求对象的实例? [重复]相关的知识,希望对你有一定的参考价值。

这个问题已经在这里有一个答案:

我使用下面的方法传递名为“客户”的对象的方法。我先前已实例customerArray作为主空对象。

Main

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

Passed Array

customerArray = Methods.NewAccount(customerArray);

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

Method

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


" +
                    $"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


" +
                    $"Please enter the Account Number for {createdCustomers[i].CustomerName}.");
                accountBalanceStr = Console.ReadLine();
            }
            createdCustomers[i].CustomerAccount.AccountNumber = accountNumberInt;
        }

    }

    return createdCustomers;
}

我试图改变物体本身内的值,并将其传递回主。每当我进入我的信息,它踢回来的错误。我不能完全肯定这是怎么回事。

我相信我的问题是数据是如何被处理的。我也尝试实例化一个新的对象,但不会做任何我好,因为我需要从之前创建的对象的所有数据。

Errors on Line

createdCustomers[i].CustomerAccount.AccountBalance = accountBalanceDec

Customer Object

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

        public CheckingAccount CustomerAccount { get; set; }

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

Checking Account Object

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

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

Created Customer

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?

这些页面简单地说是什么原因造成的错误,但只能在低层次手段。我使用数组和方法,通过这些阵列。我无法理解的是方法处理数组和对象如何需要实例化这样的方式。这个问题是不知道是什么原因造成的错误,这是“确定”不是一个简单的裁判错误更复杂的事情。

答案

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

createdCustomers[i].CustomerAccount.AccountBalance可分为

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

因此NullReferenceException

另一答案

的建议夫妇:

首先需要将数组传递作为ref/out如果你正在试图改变这些值并返回同一阵列。其次,需要在客户对象中初始化CheckingAccount

所以,

createdCustomers[i].CustomerAccount = new CheckingAccount(..);

参考在C#:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

以上是关于对象数组传递作为参数,请求对象的实例? [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何创建对象的新实例并将其传递到数组 SwiftUI

将类对象实例作为参数从 main() 传递给另一个类对象实例

如何访问对象而不将其作为参数传递?

如何将对象属性作为参数传递? (JavaScript)

传递对象作为未更新的参数[重复]

将对象实例直接作为 const 接口参数传递时,编译器是不是应该提示/警告?