无法通过NullReferenceException

时间:2013-08-24 18:43:22

标签: c# nullreferenceexception

我是编程新手,特别是在c#中。我已经编写了一些代码,但是在运行时我一直遇到错误,直到我得到修复后才能继续运行。

有问题的错误是NullReferenceException。它还告诉我“对象引用未设置为对象的实例”。

这似乎是一个非常明确的错误消息,表明尚未实例化对象。但是我以为我已经这样做了。我希望有人可以向我解释我做错了什么。这是我的代码。

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++;
                            }
                        }
                    }
                }
            }
        }
   }
}

错误消息指示的行靠近底部并显示values[panelX, panelY][squareX, squareY] = conversion[count];

1 个答案:

答案 0 :(得分:6)

问题是以下一行

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

这是一个数组数组,但此代码仅创建外部数组。内部数组未初始化,将为null。因此,当以下代码运行时,它将抛出NullReferenceException试图访问内部数组

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

要解决此问题,只需在第3个嵌套循环

之前初始化数组元素
values[panelX, panelY] = new int[factor, factor];
for (int squareX = 0; squareX < factor; squareX++)