我正在使用静态类作为某种库(可能不是最好的主意,但我不知道自己在做什么……)我有一些带有对象的字段,所以我可以很容易地按名称访问它们。但是我仍然需要一个数组,我可以使用一个方法来搜索。
但是,当我调用该方法GetTypeByName(“ironMine”)时,它应该通过数组返回查找并返回ironMine对象,但是它只是尖叫“对象引用没有设置为对象的实例”.我显然遗漏了一些关于静态数组或数组的东西,我还没有习惯它们.
public static class IndustryTypes
{
public static IndustryType[] allTypes = //This is that array that seems to mess things up...
{
ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
quarry, oilWell,
farm
};
public static IndustryType noType;
public static IndustryType ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine;
public static IndustryType quarry, oilWell;
public static IndustryType farm;
static IndustryTypes()
{
noType = new IndustryType("noType", 0, Color.Red);
ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);
quarry = new IndustryType("Quarry", 25000, Game.color_mine);
oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
farm = new IndustryType("Farm", 10000, Game.color_farm);
}
public static IndustryType GetTypeByName(string name)
{
for (int i = 0; i < allTypes.Length; i++)
{
if (name == allTypes[i].name) //This says "Object reference not set to an instance of an object" or something like that...
{
return allTypes[i];
}
}
return noType;
}
}所有这些都在一个名为"IndustryTypes“的静态类中,所以我不需要实例化它。
类"IndustryType“是一个非静态类。
如果你不明白我的意思..。我不太了解自己,但我会尽力的!
这里是"IndustryType"-class:
public class IndustryType
{
public string name;
public int cost;
public Color color;
public List<Resource> components;
public List<Resource> products;
public IndustryType(string _name, int _cost, Color _color)
{
name = _name;
cost = _cost;
color = _color;
}
}非常感谢你抽出时间来!
发布于 2017-02-25 17:23:04
在初始化所有静态字段后,将执行IndustryTypes类的静态构造函数。所以,现在,当这件事被执行时:
public static IndustryType[] allTypes = //This is that array that seems to mess things up...
{
ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
quarry, oilWell,
farm
};所有字段(如ironMine等)仍未初始化并指向null。
解决方案:将字段的初始化从静态构造函数移动到声明本身。还要注意,初始化器是按照文本顺序执行的,所以必须在所有其他字段之后最后声明数组。
public static class IndustryTypes
{
public static IndustryType noType = new IndustryType("noType", 0, Color.Red);
public static IndustryType ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
public static IndustryType coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
public static IndustryType aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
public static IndustryType copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
public static IndustryType uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
public static IndustryType goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);
public static IndustryType quarry = new IndustryType("Quarry", 25000, Game.color_mine);
public static IndustryType oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
public static IndustryType farm = new IndustryType("Farm", 10000, Game.color_farm);
public static IndustryType[] allTypes = {
IndustryTypes.ironMine,
IndustryTypes.coalMine,
IndustryTypes.aluminiumMine,
IndustryTypes.copperMine,
IndustryTypes.uraniumMine,
IndustryTypes.goldMine,
IndustryTypes.quarry,
IndustryTypes.oilWell,
IndustryTypes.farm
};
}发布于 2017-02-25 17:23:01
将数组定义为空引用数组。将数组构造移到静态构造函数的末尾。
public static IndustryType[] allTypes;
public static IndustryType noType;
public static IndustryType ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine;
public static IndustryType quarry, oilWell;
public static IndustryType farm;
static IndustryTypes() {
noType = new IndustryType("noType", 0, Color.Red);
ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);
quarry = new IndustryType("Quarry", 25000, Game.color_mine);
oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
farm = new IndustryType("Farm", 10000, Game.color_farm);
allTypes = new [] {
ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
quarry, oilWell, farm };
}如果您永远不会直接访问这些公共字段(即通过IndustryTypes.goldMine ),那么您就可以删除它们,并一次性将它们分配给数组,而不需要字段。
allTypes=new[] {
new IndustryType("noType", 0, Color.Red),
new IndustryType("Iron Mine", 50000, Game.color_mine),
new IndustryType("Coal Mine", 40000, Game.color_mine),
new IndustryType("Aluminium Mine", 100000, Game.color_mine),
new IndustryType("Copper Mine", 55000, Game.color_mine),
new IndustryType("Uranium Mine", 150000, Game.color_mine),
new IndustryType("Gold Mine", 125000, Game.color_mine),
new IndustryType("Quarry", 25000, Game.color_mine),
new IndustryType("Oil Well", 110000, Game.color_oil),
new IndustryType("Farm", 10000, Game.color_farm)
};如果可以的话,我建议在字段上使用公共属性,并将所有这些都移动到IndustryType类(而不是复数)中。
注意到,您的数组已公开。它不是只读的,它的元素可以更改。最好将其封装在公开为IReadOnlyXXX接口之一的属性之后。否则,没有您的知识/权限,就可以直接执行IndustryTypes.allTypes[0]=null; (或者更糟的是,将其设置为新的IndustryType实例)。
https://stackoverflow.com/questions/42458984
复制相似问题