0

I am still somewhat new to C#, and I ran into a problem I don't understand. I am trying to add an object to a list to test my program. I have a parent class called "Inventory" the code is as follows:

namespace BeerScribeDesktop
{
public class Inventory
{
    private string _name;
    private double _amount;
    private string _supplier;
    private double _reorderAmount;


    /// <summary>
    /// getter setter _name
    /// </summary>
    public string Name
    {
        get { return _name;}
        set { _name = value; }
    }

    /// <summary>
    /// getter setter _supplier
    /// </summary>
    public string Supplier
    {
        get { return _supplier; }
        set { _supplier = value; }
    }

    /// <summary>
    /// Getter/Setter for _amount
    /// </summary>
    public double Amount
    {
        get { return this._amount; }
        set { this._amount = value; }
    }

    /// <summary>
    /// getter setter _reorderAmount
    /// </summary>
    public double Reorder
    {
        get { return this._reorderAmount; }
        set { this._reorderAmount = value; }
    }

}

then I have a child that is suppose to inherit from the parent its code is:

namespace BeerScribeDesktop
{
public class Adjuncts: Inventory
{
    private string _type;

    /// <summary>
    /// 4 parameter construcor for the Adjunct class
    /// </summary>
    /// <param name="type"> Type(class) of adjunct </param>
    /// <param name="name"> Name of the product </param>
    /// <param name="amount"> Amount of inital input </param>
    /// <param name="supplier"> supplier name </param>
    public Adjuncts(string type, string name, double amount, string supplier)
    {
        this._type = type;
        base.Name = name;
        base.Amount = amount;
        base.Supplier = supplier;
    }

    /// <summary>
    /// 6 parameter constructor for the adjunct class
    /// </summary>
    /// <param name="type"> Type(class) of adjunct </param>
    /// <param name="name"> Name of the product </param>
    /// <param name="amount"> Amount of inital input </param>
    /// <param name="supplier"> Supplier name </param>
    /// <param name="reorder"> Lowest amount before reorder </param>
    public Adjuncts(string type, string name, double amount, string supplier, double reorder)
    {
        this._type = type;
        base.Name = name;
        base.Amount = amount;
        base.Supplier = supplier;
        base.Reorder = reorder;
    }

    /// <summary>
    /// Getter/Setter for the _type field
    /// </summary>
    public string Type
    {
        get { return this._type; }
    } 

and im trying to add to the list with this code:

namespace BeerScribeDesktop
{
public partial class BSDesktopParent : Form
{
    private int childFormNumber = 0;
    private NewInventoryItem nii;
    private InventoryView iv;


    private List<Adjuncts> _adjunctList;
    private List<BrewersAids> _brewerAidList;
    private List<Chemicals> _chemicalList;
    private List<Hops> _hopList;
    private List<Malts> _maltList;
    private List<Other> _otherList;
    private List<Sugars> _sugarList;
    private List<WaterTreatment> _waterTreatmentList;
    private List<YeastNutrients> _yeastNutrientList;

    public BSDesktopParent()
    {
        InitializeComponent();
        // *******************************************************TESTING PURPOSES
        Adjuncts adj1 = new Adjuncts("Flaked Barley", "Test1 Name", 100.00, "test1 Supplier");
        Adjuncts adj2 = new Adjuncts("Flaked Barley", "Test2 Name", 200.00, "test2 Supplier");
        Adjuncts adj3 = new Adjuncts("Flaked Corn", "Test3 Name", 300.00, "test3 Supplier");
        Adjuncts adj4 = new Adjuncts("Flaked Corn", "Test4 Name", 400.00, "test4 Supplier");
        _adjunctList.Add(adj1);
        _adjunctList.Add(adj2);
        _adjunctList.Add(adj3);
        _adjunctList.Add(adj4);
        //********************************************************END TESTING PURPOSES
    }

when I go to add to the list, i get a null pointer exception. I don't understand why I am getting this. This is my first time making a class that inherits from another class, and I think this is where its going wrong.

thank you for any help ahead of time!

Blorgbeard
  • 93,378
  • 43
  • 217
  • 263
Michael
  • 21
  • 1

3 Answers3

2

You need to instantiate all of your lists. Here is an example for the first one:

_adjunctList = new List< Adjuncts >();

Next time you have your beer read this article about C# initializers.

Nix
  • 52,320
  • 25
  • 137
  • 193
1

_adjunctList is null because you are not initializing it. Do this in BSDesktopParent():

_adjunctList = new List<Adjuncts>();
JLRishe
  • 90,548
  • 14
  • 117
  • 150
1

You have not initialized _adjunctList. Before adding an item do this:

_adjunctList = new List<Adjuncts>();
Hintham
  • 998
  • 10
  • 25