Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Arrays Jagged Arrays

Karlijn Willems
Karlijn Willems
9,961 Points

Trying to create an array of arrays... Stuck on the error: Object reference not set to an instance of an object

I don't know if I'm getting any closer with these loops. I don't really understand how to add any content (an integer) in a cell of the table.

Also I am getting the error: Bummer! Object reference not set to an instance of an object. It is not referring to a codeline and I don't get what it is pointing to so I'm stuck here...

I appreciate any help! :)

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] table = new int[maxFactor][];
            for (int rowIndex = 0; rowIndex < table[rowIndex].Length; rowIndex++)
            {
                table[rowIndex] = new int[maxFactor +1];
                for (int colIndex = 0; colIndex < table[rowIndex].Length; colIndex++)
                {
                    int cell = rowIndex * colIndex;
                    table[rowIndex][colIndex] = cell;
                }
            }
            return table;
        }

    }
}

3 Answers

Steven Parker
Steven Parker
231,269 Points

When you initialize your individual rows, you correctly set the size to one more than the highest factor. But you didn't do that for the outer table.

Also, in the outer (rowIndex) loop, you use the length of individual rows in the conditional clause instead of the length of the table itself.

Karlijn Willems
Karlijn Willems
9,961 Points

Thank you! I think I changed it with all the corrections you gave me. I also checked the code on jagged arrays in c# 7 in a Nutshell... but still getting the same error. This is my code now:

public static class MathHelpers { public static int[][] BuildMultiplicationTable(int maxFactor) { int[][] jaggedArray = new int[maxFactor + 1][];

        for (int i = 0; i < jaggedArray.Length; i++)
        {
            jaggedArray[i] = new int[maxFactor + 1];
            for (int j = 0; j < jaggedArray[maxFactor].Length; j++)
            {
                jaggedArray[i][j] = i * j;
            }
        }
        return jaggedArray;
    }

}

update: sorry it only recognizes part of the code as code...

Steven Parker
Steven Parker
231,269 Points

To make your code look like what the "Get Help" button did, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Not counting variable names, t appears you made an additional change that wasn't one of the suggestions. Now the inner loop is using "maxFactor" in the limit condition instead of "rowIndex" (now "i"). That row won't be established until the last iteration of the outer loop.

Karlijn Willems
Karlijn Willems
9,961 Points

Thnx for the the explanation on the code formatting :)

Your right, it works! I was playing around with the variablenames a bit too much I guess... Thanks again!