[英]The Field Is Never Assigned To And Will Always Have Its Default Value null
I currently have 2 bool arrays in a class file as defined by 我目前在类文件中有2个布尔数组,定义如下
public static bool[] bArray;
public static bool[] randomRemove;
And I fill bArray
like this 我这样填充
bArray
public static void fillArray()
{
for (int x = 0; x < 54; x++)
{
bArray[x] = false;
}
}
And I fill randomRemove
like this 然后我像这样填充
randomRemove
for (int i = 0; i < sizeOfArray; i++)
{
randomRemove[i] = false;
}
Where sizeOfArray
is the length of string array that I use. 其中
sizeOfArray
是我使用的字符串数组的长度。
I have two warnings for each bool array that says they are never assigned to and will always have its default value of null but I clearly have code that assigns them. 对于每个布尔数组,我有两个警告,表示它们从未分配给它们,并且始终将其默认值设置为null,但是我显然有分配它们的代码。 Whenever I try to click a button that utilizes the arrays I get a run time error.
每当我尝试单击一个利用数组的按钮时,都会出现运行时错误。 Is there any reason this is happening?
这有什么原因吗?
You need to call 你需要打电话
bArray = new bool[sizeOfArray];
somewhere in your code before you use them. 使用它们之前,在代码中的某个位置。 Also, bool arrays default to all falses.
同样,布尔数组默认为所有false。
You are not instantiating your arrays - see http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx for more info. 您没有实例化数组-有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/aa288453(v=vs.71).aspx 。
eg 例如
public static bool[] bArray = new bool[sizeOfArray];
public static bool[] randomRemove = new bool[sizeOfArray];
You did not create instances of the arrays. 您没有创建阵列的实例。 Try
尝试
public static bool[] bArray = new bool[54]();
public static bool[] randomRemove = new bool[sizeofarray]();
instead. 代替。
You need to initialize the arrays in your constructor or where it makes sense. 您需要在构造函数中或有意义的地方初始化数组。
public class MyClass
{
public static bool[] bArray;
static MyClass()
{
bArray = new bool[54];
}
}
In your code you are only assigning the items in the array which will give you a NullReferenceException because your array = null. 在您的代码中,您仅分配数组中的项目,这将为您提供NullReferenceException,因为您的array = null。 You need to initialize the array.
您需要初始化数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.