I am trying to create a Sudoku in WinForms with C# as a school assignment. 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:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication5.exe Additional information: Object reference not set to an instance of an object.
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
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