Hello. I write the game "Minesweeper", working from the console window. An error occurs on the code section:

field = new cell[ySize][]; for (int i = 0; i<ySize; i++) field[i] = new cell[xSize]; // инициализация Π³Π΅Π½Π΅Ρ€Π°Ρ‚ΠΎΡ€Π° случайных чисСл Random rnd = new Random(); int n=0; // расставим ΠΌΠΈΠ½Ρ‹ do { int row= rnd.Next(ySize); //ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° ΠΌΠΈΠ½Ρ‹ ΠΏΠΎ Y int col=rnd.Next(xSize); //ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° ΠΌΠΈΠ½Ρ‹ ΠΏΠΎ X if (field[row][col].cheсkNearCell()!= (-1)) { field[row][col].setMine(); if (row != 0 && col != 0) field[row - 1][col - 1].newMineNear(); if (row != 0) field[row - 1][col].newMineNear(); if (row != 0 && col != xSize - 1) field[row-1][col + 1].newMineNear(); if (col != 0) field[row][col - 1].newMineNear(); if (col != xSize - 1) field[row][col + 1].newMineNear(); if (row != ySize - 1 && col != 0) field[row + 1][col-1].newMineNear(); if (row != ySize - 1) field[row + 1][col].newMineNear(); if ((row != ySize-1) && (col != xSize-1)) field[row + 1][col + 1].newMineNear(); n++; } } while (n != numberOfMines); 

In this section of the code, the array containing the β€œcells” of which the field consists of must be filled, and mines are placed. The code is compiled, the game starts, but gives an error

System.NullReferenceException: The object reference does not indicate an object instance.

I suspect that when the array array is populated, the class constructor is not called. Here is the Cell class code:

 class cell { private bool opend {get; set;} // - посСщали Π»ΠΈ ΠΌΡ‹ ΠΊΠ»Π΅Ρ‚ΠΎΡ‡ΠΊΠ΅ private bool hasMine {get; set;} // - установлСна Π»ΠΈ Π² ΠΊΠ»Π΅Ρ‚ΠΎΡ‡ΠΊΠ΅ ΠΌΠΈΠ½Π° private bool hasFlag {get; set;} // - установлСн Π»ΠΈ Π² ΠΊΠ»Π΅Ρ‚ΠΎΡ‡ΠΊΠ΅ Ρ„Π»Π°Π³ private int minesNear {get; set;} // - ΠΌΠΈΠ½Ρ‹ рядом private cell() { hasMine=false; opend=false; hasFlag=false; minesNear=0; } //ΡƒΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ Ρ„Π»Π°Π³ public int setFlag() { if (hasMine==true) {hasFlag=true; return 1;} //1 - ΠΌΠΈΠ½Π° установлСна ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ else return 0; } //установка ΠΌΠΈΠ½Ρ‹ public void setMine() { hasMine=true; } //Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ ΠΎ ΠΌΠΈΠ½Π΅ поблизости public void newMineNear() { minesNear++; } //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° этой ΠΊΠ»Π΅Ρ‚ΠΊΠΈ Π½Π° Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ ΠΌΠΈΠ½Ρ‹ public int checkThisCell() { if(hasMine==true) return 666; // 666 - ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ ΠΌΠΈΠ½Ρ‹ Π² Π΄Π°Π½Π½ΠΎΠΉ ΠΊΠ»Π΅Ρ‚ΠΊΠ΅ else if (opend==false) {opend=true; return minesNear;} else return -1; //-1 - ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Ρ‡Ρ‚ΠΎ ΠΌΡ‹ посСщали эту ΠΊΠ»Π΅Ρ‚ΠΊΡƒ ΠΈ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ Π΄Ρ€ΡƒΠ³ΡƒΡŽ ΠΊΠ»Π΅Ρ‚ΠΊΡƒ } //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° сосСднСй ΠΊΠ»Π΅Ρ‚ΠΊΠΈ Π½Π° Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ ΠΌΠΈΠ½Ρ‹ public int cheсkNearCell() { if (hasMine==true || opend==true) return -1; // -1 - Ρ‡Ρ‚ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ эту ΠΊΠ»Π΅Ρ‚ΠΊΡƒ else return minesNear; } //отрисовка ΠΊΠ»Π΅Ρ‚ΠΊΠΈ public void show() { if (hasFlag==true) Console.Write("F"); // - Π² ΠΊΠ»Π΅Ρ‚ΠΊΠ΅ установлСн Ρ„Π»Π°Π³ else if (opend==false) Console.Write("#"); // - ΠΊΠ»Π΅Ρ‚ΠΊΠ° Π½Π΅ ΠΎΡ‚ΠΊΡ€Ρ‹Π²Π°Π»Π°ΡΡŒ Ρ€Π°Π½Π΅Π΅ else if (minesNear==0) Console.Write(" "); // - рядом Π½Π΅Ρ‚ ΠΌΠΈΠ½ else Console.Write(minesNear); // - Π΅ΡΡ‚ΡŒ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½ΠΎΠ΅ количСство ΠΌΠΈΠ½ } } 
  • one
    Maybe run in debug mode to immediately see exactly which line of error and where exactly is null ? - Surfin Bird
  • @SurfinBird debugger indicates an error in the if (field[row][col].cheсkNearCell()!= (-1)) and the entire value of the field [] [] array is null - Gidemin
  • @SurfinBird Thank you for your help. - Gidemin

2 answers 2

@Surfin Bird's answer is correct, let me explain what is happening.

You declared an array of arrays . For an array, you need to allocate memory with new , but the elements of your array are one-dimensional arrays! Under each of them also need to allocate memory. Such picture will turn out:

 field ---> [ элСмСнт 0 ][ элСмСнт 1 ][ элСмСнт 2 ][ элСмСнт 3 ]... | | | | | | | | VVVV [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 

You have created the top line, but not the rest.


An alternative solution could be to select a rectangular multidimensional array. In this case, you immediately get the necessary elements:

 Cell[,] field; // ... field = new Cell[xSize][ySize]; 
  • one
    The rectangular array is even more logical here, as in any games based on a rectangular board in general. And if you consider that the moves are made by a person and the size of the board is not too large << 100k elements, then you can not think about the efficiency of processing a multidimensional array and improve the readability of the algorithm. - rdorn

It seems that the initialization should be replaced by:

 for (int i = 0; i < ySize; i++){ field[i] = new cell[xSize]; for (int j = 0; j < xSize; j++) field[i][j] = new cell(); } } 
  • In the string field[i][j] = new cell(); an error occurs> Error CS0122: "Mineswepper.cell.cell ()" is unavailable due to its level of protection. Assembly failed. (00: 00: 00.4056007) - Gidemin
  • one
    @Gidemin: Well, yes, you yourself declared the constructor private. Make it available. - VladD
  • @VladD yes, thanks for the help. I should have been more attentive. - Gidemin
  • one
    @Gidemin: Please! This is normal, most of the errors are so small. - VladD