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

What have I done wrong ? I am getting "Object reference not set to an instance of an object" error

I have searched some time on the web and couldn't find anything

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] Multiplication = new int[maxFactor + 1][];
            for(int index = 0; index < Multiplication.Length; index++)
            {
               if (index == 0)
               {
                 for(int value = 0; value < Multiplication[index].Length; value++)
                 {
                    Multiplication[index][value] = 0;  
                 }
               }
               else if(index == 1)
               {
                   for(int value = 0; value < Multiplication[index].Length; value++)
                   {
                     Multiplication[index][value] = value;   
                   }
               }    
               else
               {
                 for(int value = 0; value < Multiplication[index].Length; value++)
                 {
                     if (value == 0)
                     {
                         Multiplication[index][value] = 0;
                     }
                     else if (value == 1)
                     {
                         Multiplication[index][value] = value;
                     }
                     else
                     {
                         Multiplication[index][value] = value * Multiplication[1][value];
                     } 
                 }   
               }
              }
            return Multiplication;
        }

    }
}

2 Answers

Steven Parker
Steven Parker
229,744 Points

You're close, but you only created one dimension of the array. On or about line 10, you need to create the arrays for the second dimension:

               Multiplication[index] = new int[maxFactor + 1];  // create 2d arrays

Thanks!