I am currently trying to make monolopy as a little project. I am trying to create an Array of Tiles however when i run the code i get a nullreferenceException because Tile[] Tiles is showing as null. Any advice would be great.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Monolopy
{
class Board
{
public Tile[] Tiles { get; }
GoTile tile1;
BuildingTile tile2;
JailTile tile3;
BuildingTile tile4;
BuildingTile tile5;
BuildingTile tile6;
GoToJailTile tile7;
BuildingTile tile8;
BuildingTile tile9;
ChanceTile tile10;
BuildingTile tile11;
BuildingTile tile12;
public Board()
{
Tile[] Tiles = {
tile1 = new GoTile(),
tile2 = new BuildingTile(2, "Old Kent Road", 20, 200),
tile3 = new JailTile(),
tile4 = new BuildingTile(4, "WhiteHall", 40, 400),
tile5 = new BuildingTile(5, "Euston Road", 50, 500),
tile6 = new BuildingTile(6, "bow Street", 60, 600),
tile7 = new GoToJailTile(),
tile8 = new BuildingTile(8, "Strand", 70, 700),
tile9 = new BuildingTile(9, "Fleet Street", 80, 800),
tile10 = new ChanceTile(),
tile11 = new BuildingTile(11, "Park Lane", 90, 900),
tile12 = new BuildingTile(12, "Mayfair", 100, 1000)
};
}
}
}
Tile[]before Tiles, in your constructor, as you are populating your local variableTileswhich only lives for the duration of the Board constructor. Remove theTile[]from your board constructor to use the tile property instead.