我对C#还是有些新手,我遇到了一个我不明白的问题。我试图将一个对象添加到列表来测试我的程序。我有一个名为" 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; }
}
}
然后我有一个孩子,假设从父母继承其代码是:
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
}
当我去添加到列表中时,我得到一个空指针异常。我不明白为什么我会这样做。这是我第一次创建一个继承自另一个类的类,我认为这是它出错的地方。
提前感谢您的任何帮助!
答案 0 :(得分:2)
答案 1 :(得分:1)
_adjunctList
为null,因为您没有初始化它。在BSDesktopParent()
中执行此操作:
_adjunctList = new List<Adjuncts>();
答案 2 :(得分:1)
您尚未初始化_adjunctList。在添加项目之前,请执行以下操作:
_adjunctList = new List<Adjuncts>();