0

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?

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:

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

Thanks!

3
  • 2
    Your color_table is never initialized to any length... You should consider declaring it as [this.precision+1][3] Commented Dec 28, 2012 at 8:18
  • It underlines the [3] in red and says Invalid rank specifier: expected ',' or ']' ... That's what I had tried before that didn't work. Commented Dec 28, 2012 at 8:25
  • @user1653653: You should leave the second arrays empty as in my example. Commented Dec 28, 2012 at 8:27

2 Answers 2

3

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

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 };
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked :) I didn't know you had to leave the second array empty and then my other problem was that I was trying to declare it outside of a method.
You have to declare it in the constructor (or a method) unless precision is a constant (private const int precision = 4;)
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 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.