开发者

The Field Is Never Assigned To And Will Always Have Its Default Value null

开发者 https://www.devze.com 2023-04-12 12:59 出处:网络
I currently have 2 bool arrays in a class file as defined by public static bool[] bArray; public static bool[] randomRemove;

I currently have 2 bool arrays in a class file as defined by

public static bool[] bArray;
public static bool[] randomRemove;

And I fill bArray like this

public static void fillArray()
{
    for (int x = 0; x < 54; x++)
    {
        bArray[x] = false;
    }
}

And I fill randomRemove like this

for (int i = 0; i < sizeOfArray; i++)
{
    randomRemove[i] = false;
}

Where sizeOfArray is the length of string array that I use.

开发者_开发知识库

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. 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.


You are not instantiating your arrays - see http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx for more info.

e.g.

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. You need to initialize the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消