简体   繁体   English

该字段永远不会分配给它,并且其默认值始终为null

[英]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.

相关问题 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段&#39;xxx&#39;永远不会分配给,并且将始终具有其默认值null - Field 'xxx' is never assigned to, and will always have its default value null 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 字段从未分配给它,并且将始终具有其默认值null - Field is never assigned to, and will always have its default value null 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null” 警告“字段 .... 从未分配给并且将始终具有默认值 null” - Warning "Fields .... is never assigned to and will always have its default value null" 字段从未分配给它,并且将始终具有其默认值 - Field is never assigned to, and will always have its default value 字段modifyDate永远不会分配给,并且始终具有其默认值0 - Field modifyDate is never assigned to, and will always have its default value 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM