添加到 List<> C# 时出现空异常

null exception when adding to List<> C#

我对 C# 还是有些陌生,我 运行 遇到了一个我不明白的问题。我正在尝试将 object 添加到列表中以测试我的程序。我有一个parentclass叫"Inventory"代码如下:

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

}

然后我有一个 child 应该继承自 parent 它的代码是:

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

我正在尝试使用以下代码添加到列表中:

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
    }

当我去添加到列表时,我得到一个空指针异常。我不明白为什么我会得到这个。这是我第一次制作从另一个 class 继承的 class,我认为这是错误的地方。

提前感谢您的帮助!

您需要实例化所有列表。这是第一个的示例:

_adjunctList = new List< Adjuncts >();

下次喝啤酒时阅读这篇关于 C# initializers 的文章。

_adjunctList 为空,因为您没有对其进行初始化。在 BSDesktopParent() 中执行此操作:

_adjunctList = new List<Adjuncts>();

您还没有初始化_adjunctList。在添加项目之前执行此操作:

_adjunctList = new List<Adjuncts>();