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); // - Π΅ΡΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΌΠΈΠ½ } }
null? - Surfin Birdif (field[row][col].cheΡkNearCell()!= (-1))and the entire value of the field [] [] array is null - Gidemin