C# 在类的自定义嵌套数组中的布尔值上出现空引用异常

C# 在类的自定义嵌套数组中的布尔值上出现空引用异常,c#,C#,我已经创建了一组自定义类,以按特定顺序包含我需要的一些信息。除了最后一个类之外,每个类下面都包含一个类数组 自定义类如下所示 public class Quote { public int ServiceQuoteId; public bool Begin = new bool(); public PricingGroup[] PricingOptionGroup = new PricingGroup[10]; } public class PricingGroup

我已经创建了一组自定义类,以按特定顺序包含我需要的一些信息。除了最后一个类之外,每个类下面都包含一个类数组

自定义类如下所示

public class Quote
{
    public int ServiceQuoteId;

    public bool Begin = new bool();

    public PricingGroup[] PricingOptionGroup = new PricingGroup[10];
}
public class PricingGroup
{
    public int ItemId;

    public string ALocation;

    public bool LocSet = new bool();

    public Product[] Group = new Product[10];
}
public class Product
{
    public int Total1;

    public ProductGroup[] Set = new ProductGroup[10];

    public string Term;
}
public class ProductGroup
{
    public string Product;

    public int Charge;

    public bool Option = new bool();
}
创建对象的实例后,如下图所示

我试着测试一个如下的布尔值

但这给了我这个错误

"An exception of type 'System.NullReferenceException' occurred in WebApplication3.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object."

我试图做的可能不可能;但从逻辑上讲,我相信这是有道理的。据我所知,new bool()初始化为false。

您已经为10个ProductOptionGroup分配了空间,但实际上没有在其中放入任何空间

以下是初始化ProductOptionGroup的一种方法:

public class Quote
{
    public int ServiceQuoteId;

    public bool Begin = new bool();

    public PricingGroup[] PricingOptionGroup = new PricingGroup[10];
    public Quote(){

        PricingOptionGroup=Enumerable.Range(0,10).Select(i=>new PricingGroup()).ToArray();
    }
}
还有一个:

public class Quote
{
    public int ServiceQuoteId;

    public bool Begin = new bool();

    public PricingGroup[] PricingOptionGroup = { 
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup()
    };
}

我同意closing.OP-您已经为数组分配了存储空间,但没有实际分配它的任何实例。
PricingOptionGroup[0]
为空。这不会初始化它吗?Quote testQuote=new Quote();是的,但不是
PricingGroup
。在Quote对象内,我执行此公共PricingGroup[]PricingOptionGroup=new PricingGroup[10];这不会初始化我的数组吗?不,不会。它会为10分配存储。您仍然需要继续,类似于:
PricingOptionGroup[0]=new PricingGroup()
。我不想听起来不屑一顾,但我认为您需要仔细阅读C#的工作原理—特别是数组和集合—然后重新审视您正在尝试做的事情。还要研究属性(get/set方法),以及值类型和引用类型之间的差异。那么我必须这样做吗?Quote testQuote=new Quote();testQuote.PRICNGOPTIONGROUP=new PricingGroup[10];更新了一种初始化数组的方法。我喜欢它。非常感谢。顺便说一句,它工作得很好。感谢您回答我的问题。他们将它标记为重复问题,但他们链接到的答案没有像这样清楚地解释数组分配。非常感谢。
public class Quote
{
    public int ServiceQuoteId;

    public bool Begin = new bool();

    public PricingGroup[] PricingOptionGroup = new PricingGroup[10];
    public Quote(){

        PricingOptionGroup=Enumerable.Range(0,10).Select(i=>new PricingGroup()).ToArray();
    }
}
public class Quote
{
    public int ServiceQuoteId;

    public bool Begin = new bool();

    public PricingGroup[] PricingOptionGroup = { 
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup(),
        new PricingGroup()
    };
}