3

I'm a newbie in programming, especially in c#. I have written some code but I keep getting an error when running it and I can't move on until I get that fixed.

The error in question is a NullReferenceException. It also tells me "Object reference not set to an instance of an object".

It seems like a pretty clear error message indicating that an object hasn't been instantiated yet. However I thought I had done that. I hope someone can explain to me what I'm doing wrong. Here's my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace EvenHelemaalOvernieuw
{
    class Globals
    {
        public static int size = 50;
        public static int factor = 3;
        public static int puzzleNumber = 1;
        public static Square[,] allSquares = new Square[Globals.factor * Globals.factor, Globals.factor * Globals.factor];
        public static String path = @"" + factor.ToString() + "\\" + puzzleNumber.ToString() + ".txt";
        public static int[,][,] values = new int[factor, factor][,];

        public Globals() { }

        public void setSize(int s)
        {
            size = s;
            if (size > 100)
            {
                size = 100;
            }
            if (size < 20)
            {
                size = 20;
            }
        }

        public void setFactor(int f)
        {
            factor = f;
            if (factor > 5)
            {
                factor = 5;
            }
            if (factor < 2)
            {
                factor = 2;
            }
        }

        public Square getSquare(int x, int y)
        {
            return allSquares[x, y];
        }

        public static void readPuzzle()
        {
            List<int> conversion = new List<int>();
            int count = 0;
            using (StreamReader codeString = new StreamReader(path))
            {
                String line = codeString.ReadToEnd();
                Array characters = line.ToCharArray();
                foreach (char a in characters)
                {
                    if (a.ToString() != ",")
                    {
                        conversion.Add(Convert.ToInt32(a));
                    }
                }
                for (int panelX = 0; panelX < factor; panelX++)
                {
                    for (int panelY = 0; panelY < factor; panelY++)
                    {
                        for (int squareX = 0; squareX < factor; squareX++)
                        {
                            for (int squareY = 0; squareY < factor; squareY++)
                            {
                                values[panelX, panelY][squareX, squareY] = conversion[count];
                                count++;
                            }
                        }
                    }
                }
            }
        }
   }
}

The line that is indicated by the error message is near the bottom and reads values[panelX, panelY][squareX, squareY] = conversion[count];.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
Rainier
  • 111
  • 1
  • 1
  • 8
  • Your sample missing code that initializes `values[panelX, panelY]` - please post that portion too. – Alexei Levenkov Aug 24 '13 at 18:45
  • 1
    You have understood the message and analyzed the problem a bit. It is so refreshing to see a question where this was done. +10 – usr Aug 24 '13 at 18:48
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:42

1 Answers1

6

The problem is the following line

public static int[,][,] values = new int[factor, factor][,];

This is an array of arrays but this code only creates the outer array. The inner array is uninitialized and will be null. Hence when the following code runs it will throw a NullReferenceException trying to access the inner array

values[panelX, panelY][squareX, squareY] = conversion[count];

To fix this just initialize the array elements right before the 3rd nested loop

values[panelX, panelY] = new int[factor, factor];
for (int squareX = 0; squareX < factor; squareX++)
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • This did the trick! Thanks for your help. My code is not doing what I expected it to, but at least now I can see what's going on and try to get that fixed. – Rainier Aug 24 '13 at 18:57