전 프로그래밍에서 초보자입니다. 특히 C#. 몇 가지 코드를 작성했지만 실행시 오류가 계속 발생하고 해결 될 때까지 계속 진행할 수 없습니다.과거에 문제가 발생했습니다. NullReferenceException이 발생했습니다.
해당 오류는 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];
를 읽는다.
샘플없는 코드 배열 요소를 초기화 해결하려면 내부 배열을
에 액세스하려고 발생합니다 'values [panelX, panelY]'- 그 부분도 올려주세요. –
메시지를 이해하고 문제를 조금 분석했습니다. 이 일이 어디에서 일어 났는지 질문하는 것은 상쾌합니다. +10 – usr
'NullReferenceException '의 거의 모든 경우는 같습니다. 일부 힌트는 "[.NET의 NullReferenceException은 무엇입니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)"를 참조하십시오. –