C#对象空引用异常 - 不确定如何设置数组值

时间:2022-01-26 16:53:52

I guess I'm not setting the array correctly or something, but this throws a "nullreferenceexception" when it gets to the line where it actually sets the new array value to the color_table array (should be the 7th and 12th lines of what you see below). How should I write this so that it works?

我想我没有正确地设置数组或者其他东西,但是当它到达实际将新数组值设置为color_table数组的行时会抛出“nullreferenceexception”(应该是你看到的第7和第12行)下面)。我该如何写这个以便它有效?

public int[] colors = new int[] { 0, 255, 0, 255, 0, 255 };

private int[][] color_table;

public void setcolors()
{
    this.color_table[0] = new int[] { 0, 0, 0 };
    for (int i = 1; i <= this.precision; i++) {
        int r = (((this.colors[1] - this.colors[0]) * ((i - 1) / (this.precision - 1))) + this.colors[0]);
        int g = (((this.colors[3] - this.colors[2]) * ((i - 1) / (this.precision - 1))) + this.colors[2]);
        int b = (((this.colors[5] - this.colors[4]) * ((i - 1) / (this.precision - 1))) + this.colors[4]);
        this.color_table[i] = new int[] { r, g, b };
    }
}

I've heard something about that you MUST initialize an array with its length before using it, but a) I don't know how to do that and b) I'm not sure if it's problem. The issue there is that I don't know what the array length is going to be. I tried this to no avail:

我听说过你必须在使用它之前用它的长度初始化一个数组,但是a)我不知道该怎么做以及b)我不确定它是否有问题。问题在于我不知道数组长度是多少。我试过这个无济于事:

private int[this.precision][3] color_table;

Thanks!

谢谢!

2 个解决方案

#1


3  

this.color_table has not been initialized. Hence you can't assign values to it.

this.color_table尚未初始化。因此,您无法为其指定值。

Did you mean something like this:

你的意思是这样的:

public void setcolors()
{
    color_table = new int[precision + 1][];
    for (int i = 1; i <= this.precision; i++)
    {
        int r = (((this.colors[1] - this.colors[0]) * ((i - 1) / (this.precision - 1))) + this.colors[0]);
        int g = (((this.colors[3] - this.colors[2]) * ((i - 1) / (this.precision - 1))) + this.colors[2]);
        int b = (((this.colors[5] - this.colors[4]) * ((i - 1) / (this.precision - 1))) + this.colors[4]);
        this.color_table[i] = new int[] { r, g, b };
    }
}

#2


0  

try to use list if you don't know the length of your array

如果您不知道阵列的长度,请尝试使用列表

    List<int[]> color_table = new List<int[]>();
...
    color_table.Add(new int[] { r, g, b });

#1


3  

this.color_table has not been initialized. Hence you can't assign values to it.

this.color_table尚未初始化。因此,您无法为其指定值。

Did you mean something like this:

你的意思是这样的:

public void setcolors()
{
    color_table = new int[precision + 1][];
    for (int i = 1; i <= this.precision; i++)
    {
        int r = (((this.colors[1] - this.colors[0]) * ((i - 1) / (this.precision - 1))) + this.colors[0]);
        int g = (((this.colors[3] - this.colors[2]) * ((i - 1) / (this.precision - 1))) + this.colors[2]);
        int b = (((this.colors[5] - this.colors[4]) * ((i - 1) / (this.precision - 1))) + this.colors[4]);
        this.color_table[i] = new int[] { r, g, b };
    }
}

#2


0  

try to use list if you don't know the length of your array

如果您不知道阵列的长度,请尝试使用列表

    List<int[]> color_table = new List<int[]>();
...
    color_table.Add(new int[] { r, g, b });