C# C# Collections Arrays Jagged Arrays

William Wendell
William Wendell
904 Points

What does "Object reference not set to an instance of an object" mean?

doesn't accept the code I have despite having what seems to be the right answer

namespace Treehouse.CodeChallenges
    public static class MathHelpers
       // private int MaxFactor {get;set;}
       // private int [][] Array = new int[MaxFactor][];

        public static int[][] BuildMultiplicationTable(int maxFactor)
            //MaxFactor = maxFactor;
            int[][] array = new int[maxFactor][];

            for(int i = 0; i < array.Length; i++)
                for(int j = 0; j < maxFactor; j++)
                    array[i][j] = i * j;
            return array;

    //public class Arrays
      //  public int[][]

1 Answer

James Churchill
James Churchill
Treehouse Teacher


The error "Object reference not set to an instance of an object" means that you're encountering a "null" value at runtime when you're expecting an object. I would double check how you're initializing the jagged array.

Sometimes I find it helpful to read Microsoft's official documentation on their MSDN website. Check out this page about jagged arrays.

Jagged Arrays (C# Programming Guide) https://msdn.microsoft.com/en-us/library/2s05feca.aspx

Thanks ~James