-1

I've created a set of custom classes to contain some information I need, in specific order. Each class, besides the last class, contains an array of the class underneath it.

The custom classes are as follows.

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();
}

After creating an instance of the object, like this (below)

Quote testQuote = new Quote();

I try to test one of the boolean values like this (below.

if (!testQuote.PricingOptionGroup[0].LocSet)

But this gives me this error.

"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."

What I'm trying to do might not be possible; but logically I believe it makes sense. To my understanding, new bool() initializes to false.

Stanfrancisco
  • 352
  • 1
  • 7
  • 24
  • 1
    I agree with the closing. OP - you've allocated *storage* for your array, but not actually allocated any instances of it. `PricingOptionGroup[0]` is null. – Moo-Juice Sep 28 '15 at 20:06
  • Does this not initialize it? Quote testQuote = new Quote(); – Stanfrancisco Sep 28 '15 at 20:07
  • Yes, but not a `PricingGroup`. – Moo-Juice Sep 28 '15 at 20:08
  • Inside the quote object I do this public PricingGroup[] PricingOptionGroup = new PricingGroup[10]; Does that not initialize my array? – Stanfrancisco Sep 28 '15 at 20:09
  • No, it does not. It allocates storage for 10. You'd still need to go, something like: `PricingOptionGroups[0] = new PricingGroup()`. Without wanting to sound dismissive, I think you need to read up on how C# works - specifically arrays and collections - and then revisit what you're trying to do. Also research properties (get/set methods), and the differences between value types and reference types. – Moo-Juice Sep 28 '15 at 20:13
  • I don't think I need to relearn C# because of one simple error. Thanks though. – Stanfrancisco Sep 28 '15 at 20:26

1 Answers1

0

You've allocated space for 10 ProductOptionGroups, but you didn't actually put any in there.

Here's one way to initialize the ProductOptionGroups:

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();
    }
}

Here's another:

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()
    };
}
Robert McKee
  • 21,305
  • 1
  • 43
  • 57