C# C# Collections Arrays Jagged Arrays

Beniamin Bibrowski
Beniamin Bibrowski
3,773 Points

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
172,127 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