Getting 'NullReferenceException' when putting int in Array of variety Object

Sadjad Johansson

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

Joel Coehoorn

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting 'NullReferenceException' when putting int in Array of variety Object

From Dev

Getting NullReferenceException when trying to add a value to an array

From Dev

Putting int variable into Array

From Dev

NullReferenceException when accessing array

From Dev

Putting an object into an array in as3

From Dev

When putting rows into array it duplicates

From Dev

Getting an exception when putting string before while

From Dev

" Object to int" operator causes NullReferenceException in c#

From Dev

Invalid literal for int() with base 10: ' when getting object's id

From Dev

getting error when trying to convert object column into int

From Dev

Putting a json object into mongoose model withour getting .save is not a function

From Dev

NullReferenceException unhandled when reached null element in array

From Dev

Getting the value of div element using javascript and putting it in a array

From Dev

Why I am getting $parse error when use it on object with array?

From Dev

Getting NullPointerException with Object Array

From Dev

Getting an error ; 'int' object not callable

From Dev

When attempting to pass an int to determine array size in an array wrapped in an object, code does not execute depending on int size?

From Dev

Java - Why am I getting this variety of errors?

From Dev

Getting smiley faces instead of 0 and 1s when converting int array to char array

From Dev

Why is NullReferenceException being returned when trying to add an object to my cache?

From Dev

Getting the variable name for NullReferenceException

From Dev

In unity why getting error NullReferenceException: Object reference not set to an instance of an object on CameraScript?

From Dev

In unity why getting error NullReferenceException: Object reference not set to an instance of an object on CameraScript?

From Dev

Getting the length of an array inside an object

From Dev

Adding object to array and getting NullPointerException

From Dev

getting object instead of array in javascript

From Dev

Trouble getting input for object array

From Dev

Getting the sub length of a Object[][] array

From Dev

Adding object to array and getting NullPointerException

Related Related

  1. 1

    Getting 'NullReferenceException' when putting int in Array of variety Object

  2. 2

    Getting NullReferenceException when trying to add a value to an array

  3. 3

    Putting int variable into Array

  4. 4

    NullReferenceException when accessing array

  5. 5

    Putting an object into an array in as3

  6. 6

    When putting rows into array it duplicates

  7. 7

    Getting an exception when putting string before while

  8. 8

    " Object to int" operator causes NullReferenceException in c#

  9. 9

    Invalid literal for int() with base 10: ' when getting object's id

  10. 10

    getting error when trying to convert object column into int

  11. 11

    Putting a json object into mongoose model withour getting .save is not a function

  12. 12

    NullReferenceException unhandled when reached null element in array

  13. 13

    Getting the value of div element using javascript and putting it in a array

  14. 14

    Why I am getting $parse error when use it on object with array?

  15. 15

    Getting NullPointerException with Object Array

  16. 16

    Getting an error ; 'int' object not callable

  17. 17

    When attempting to pass an int to determine array size in an array wrapped in an object, code does not execute depending on int size?

  18. 18

    Java - Why am I getting this variety of errors?

  19. 19

    Getting smiley faces instead of 0 and 1s when converting int array to char array

  20. 20

    Why is NullReferenceException being returned when trying to add an object to my cache?

  21. 21

    Getting the variable name for NullReferenceException

  22. 22

    In unity why getting error NullReferenceException: Object reference not set to an instance of an object on CameraScript?

  23. 23

    In unity why getting error NullReferenceException: Object reference not set to an instance of an object on CameraScript?

  24. 24

    Getting the length of an array inside an object

  25. 25

    Adding object to array and getting NullPointerException

  26. 26

    getting object instead of array in javascript

  27. 27

    Trouble getting input for object array

  28. 28

    Getting the sub length of a Object[][] array

  29. 29

    Adding object to array and getting NullPointerException

HotTag

Archive