Hello everyone. I am writing a game “Minesweeper”, working from the console window. An error occurs in a piece of code:
field = new cell [ySize] [];
for (int i = 0; i & lt; ySize; i ++)
field [i] = new cell [xSize];
// initialize the random number generator
Random rnd = new Random ();
int n = 0;
// place mines
do
{
int row = rnd.Next (ySize); // mine Y coordinate
int col = rnd.Next (xSize); // mine X coordinate
if (field [row] [col] .chewithkNearCell ()! = (-1))
{
field [row] [col] .setMine ();
if (row! = 0 & amp; & amp; col! = 0) field [row - 1] [col - 1] .newMineNear ();
if (row! = 0) field [row - 1] [col] .newMineNear ();
if (row! = 0 & amp; & amp; 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 & amp; & amp; col! = 0) field [row + 1] [col-1] .newMineNear ();
if (row! = ySize - 1) field [row + 1] [col] .newMineNear ();
if ((row! = ySize-1) & amp; & amp; (col! = xSize-1)) field [row + 1] [col + 1] .newMineNear ();
n ++;
}
}
while (n! = numberOfMines);
In this part of the code, an array containing the “cells” of which the field is composed should be filled, and mines should be placed. The code compiles, the game starts, but gives an error
System.NullReferenceException: An object reference does not point to an instance of an object.
I suspect that no class constructor is called when populating an array of arrays. Here is the code for the Cell class:
class cell
{
private bool opend {get; set;} // - have we visited the cell
private bool hasMine {get; set;} // - is a mine installed in the cell
private bool hasFlag {get; set;} // - is the flag set in the cell
private int minesNear {get; set;} // - mines nearby
private cell ()
{
hasMine = false;
opend = false;
hasFlag = false;
minesNear = 0;
}
// set the flag
public int setFlag ()
{
if (hasMine == true)
{hasFlag = true; return 1;} // 1 - mine was set successfully
else return 0;
}
// install mine
public void setMine ()
{
hasMine = true;
}
// add information about a mine nearby
public void newMineNear ()
{
minesNear ++;
}
// check this cell for a mine
public int checkThisCell ()
{
if (hasMine == true)
return 666; // 666 - means the presence of a mine in this cell
else if (opend == false)
{opend = true; return minesNear;}
else return -1; // - 1 - means that we visited this cell and we need to select another cell
}
// check an adjacent cell for a mine
public int chewithkNearCell ()
{
if (hasMine == true || opend == true)
return -1; // -1 - what to ignore this cell
else return minesNear;
}
// draw the cell
public void show ()
{
if (hasFlag == true) Console.Write ("F"); // - the flag is set in the cell
else if (opend == false)
Console.Write ("#"); // - the cell was not opened earlier
else if (minesNear == 0)
Console.Write (""); // - there are no mines nearby
else Console.Write (minesNear); // - there is a certain number of mines
}
}
Answer 1, authority 100%
@ Surfin Bird’s answer is correct, let me explain what’s going on.
You have declared an array of arrays . You need to allocate memory for an array using new
, but the elements of your array are one-dimensional arrays! For each of them, you also need to allocate memory. You will get the following picture:
field --- & gt; [item 0] [item 1] [item 2] [item 3] ...
| | | |
| | | |
V V V V
[] [] [] []
[] [] [] []
[] [] [] []
[] [] [] []
You created the top line, but not the rest.
An alternative solution could be to allocate a rectangular multidimensional array. In this case, you immediately get the elements you need:
Cell [] field;
// ...
field = new Cell [xSize] [ySize];
Answer 2, authority 67%
It looks like initialization needs to be replaced with:
for (int i = 0; i & lt; ySize; i ++) {
field [i] = new cell [xSize];
for (int j = 0; j & lt; xSize; j ++)
field [i] [j] = new cell ();
}
}