[英]Getting 'NullReferenceException' when putting int in Array of variety Object
I am trying to create a Sudoku in WinForms with C# as a school assignment. 我试图在WinForms中使用C#作为学校作业创建Sudoku。 Everything in the sudoku MUST be object oriented so I have not chosen to structure the code like this, the teacher did. 数独中的所有内容都必须是面向对象的,因此老师没有选择构建这样的代码。
When I put a number(int) in a Textbox in the SudokuGUI, it tries to put the number in the arrays but fails and give me the error well known: 当我在SudokuGUI的文本框中放置一个数字(int)时,它尝试将数字放置在数组中,但失败并给我众所周知的错误:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication5.exe Additional information: Object reference not set to an instance of an object. WindowsFormsApplication5.exe中发生了类型为'System.NullReferenceException'的未处理异常。其他信息:对象引用未设置为对象的实例。
This is how the code look like: 代码如下所示:
First we send the Number when keyreleased from the TextBox to the method that will put the number in to the array 首先,将从文本框释放键时的数字发送到将数字放入数组的方法
private void Valuechange_KeyUp(object sender, KeyEventArgs e) { TextBox text_box = sender as TextBox; var position = tableLayoutPanel1.GetPositionFromControl(text_box); int x = position.Row; int y = position.Column; if (int.TryParse(text_box.Text, out value) && int.Parse(text_box.Text) < 10 && int.Parse(text_box.Text) > 0 || value == 0) { add_value.Array_AddNumber(x, y, value); } else { MessageBox.Show("Skriv in en siffra mellan 1-9"); text_box.Clear(); } }
Here is the method that will add the number from Textbox to the Array that will hold the numbers 这是将文本框中的数字添加到保存数字的数组的方法
class Ruta { Siffra number = new Siffra(); public Siffra[,] SudokuArray = new Siffra[9, 9]; public void Array_AddNumber(int x, int y, int value) { SudokuArray[x, y].nummer = value; } }
And here is the "Siffra" which means Number in Swedish, that is the the type of the Array 这是“ Siffra”,在瑞典语中表示Number,即数组的类型
class Siffra { private int _nummer; public int nummer { get { return _nummer; } set { _nummer = value; } } }
What have I done wrong, I really don't understand, my teacher couldn't even help me :/ 我做错了什么,我真的不明白,我的老师甚至都帮不了我:/
Here is the whole solution: https://dl.dropboxusercontent.com/u/13409794/WindowsFormsApplication5.zip 这是整个解决方案: https : //dl.dropboxusercontent.com/u/13409794/WindowsFormsApplication5.zip
You have allocated the array to have a size that can hosts 9x9 Siffra, and that is right, but the 81 slots present in the array are all NULL. 您已将阵列分配为具有可容纳9x9 Siffra的大小,这是正确的,但是阵列中存在的81个插槽全部为NULL。
None contains a Siffra
so, when your code executes 没有包含Siffra
因此在执行代码时
SudokyArray[x,y].nummer = value;
it is like you are writing 就像你在写
null.nummer = value;
of course this is a NullReferenceException
当然这是一个NullReferenceException
Somewhere, possibly in the constructor of your class Ruta
you need to fill the array with 81 instances of the class Siffra
在某个地方,可能是在类Ruta
的构造函数中,您需要用Siffra
类的81个实例填充数组
class Ruta
{
public Siffra[,] SudokyArray;
public Ruta()
{
SudokyArray = new Sufra[9,9]
for(int i = 0; i < 9; i++)
{
for(int y = 0; y < 9; y++)
SudokuArray[i, y] = new Suffra();
}
}
}
Since SudukuArray isn't null, the problem (the null value) must be the thing in it. 由于SudukuArray不为null,因此问题(null值)必须是其中的东西。
Siffra is a class - a reference type. Siffra是一个类-引用类型。 That means instances of it are null by default (unlike structs, or value types). 这意味着默认情况下,它的实例为null(与结构或值类型不同)。
So when you create a 9x9 array of them, you are creating a 9x9 array of nulls. 因此,当您创建一个9x9的数组时,您将创建一个9x9的null数组。
The rest is homework. 剩下的就是功课了。
You are initializing the array: 您正在初始化数组:
public Siffra[,] array = new Siffra[9,9];
But never creating individual Siffra
instances. 但切勿创建单个Siffra
实例。 Therefore, when you attempt to access one, you are actually getting a null
. 因此,当您尝试访问一个时,实际上得到的是null
。 You then attemp to get a nummer
from the null instance... which leads to the exception. 然后,您学尝试得到一个nummer
从空实例......这导致了异常。
Solution 解
Initialize each instance in the array before you use it: 在使用数组之前,请对其进行初始化:
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
array[i,j] = new Siffra();
The problem is a misunderstanding of this line: 问题是对这一行的误解:
public Siffra[,] SudokuArray = new Siffra[9,9];
That line creates a new 2-dimensional array object in memory, with space for 9 items x 9 items (81 in total). 该行将在内存中创建一个新的二维数组对象,该空间可容纳9个项目x 9个项目(总共81个)。 The misunderstanding is that the contents of each spot in the array is still null
. 误解是数组中每个点的内容仍然为null
。 Therefore, later on in your code, when you do this: 因此,稍后在您的代码中,当您这样做时:
SudokuArray[x,y].nummer = value;
The code first looks up the array reference and uses that to find the element at position (x,y). 代码首先查找数组引用,并使用该引用在位置(x,y)处查找元素。 That value is still null
. 该值仍为null
。 The code then ties to use the nummer
property of a null
reference. 然后,代码nummer
为使用null
引用的nummer
属性。 Oops. 哎呀。 You can't do that. 你不能那样做。
To fix it, you need to add this code to the constructor for your Ruta
class: 要修复它,您需要将此代码添加到您的Ruta
类的构造函数中:
for (int x = 0; x < 9; x++)
for (int y = 0; y < 9; y++)
SudokuArray[x,y] = new Siffra();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.