1

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:

  1. 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();
        }
    
    }
    
  2. 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;
        }
    }
    
  3. 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

4
  • 5
    Screen shots of code are a pain for us to work with here. Copy/paste the code into your questions. Commented Jan 10, 2014 at 21:11
  • If you feel you need to zip up your entire project and post it for us, you haven't narrowed down the problem anywhere near where you should. Commented Jan 10, 2014 at 22:11
  • @JoelCoehoorn I replaced the pictures with code, will make this thread easier to find if someone gets the same newbie error as I got. Commented Jan 11, 2014 at 12:50
  • @tnw The .zip was just something i added because I thought it would maybe make it easier for you guys to understand my problem if you got the whole picture. Commented Jan 11, 2014 at 12:53

4 Answers 4

1

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). The misunderstanding is that the contents of each spot in the array is still 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). That value is still null. The code then ties to use the nummer property of a null reference. Oops. You can't do that.

To fix it, you need to add this code to the constructor for your Ruta class:

for (int x = 0; x < 9; x++)
  for (int y = 0; y < 9; y++)
     SudokuArray[x,y] = new Siffra();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help and for the very good explenation. "...contents of each spot in the array is still null" that lines made me understand everything. For me It's kind of complicated to work and understand Object Oriented Programming in C#. The Theoretical parts is kind of easy to read and understand but when it comes the programming it gets harder for me but this was one step forward in the right direction for me thanks to you! If you know any good videos, books or articles about the practical part in OO-Programming in C#, hit me back! Thank you again
1

Since SudukuArray isn't null, the problem (the null value) must be the thing in it.

Siffra is a class - a reference type. That means instances of it are null by default (unlike structs, or value types).

So when you create a 9x9 array of them, you are creating a 9x9 array of nulls.

The rest is homework.

Comments

1

You are initializing the array:

public Siffra[,] array = new Siffra[9,9];

But never creating individual Siffra instances. Therefore, when you attempt to access one, you are actually getting a null. You then attemp to get a nummer from the null instance... which leads to the exception.

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();

Comments

1

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.

None contains a Siffra so, when your code executes

SudokyArray[x,y].nummer = value; 

it is like you are writing

null.nummer = value; 

of course this is a NullReferenceException

Somewhere, possibly in the constructor of your class Ruta you need to fill the array with 81 instances of the class Siffra

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();
       }
    }
}

1 Comment

Thank you for your explanation! The part with "null.nummer = value;" was excellent and made it easier for me to understand. Like I said to the guy above you, the Theoretical parts is kind of easy to read and understand but when it comes the programming it gets harder. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.