-2

I am making a game, and I am trying to make my game moddable, so I am making that the game reads a list of possible buildings and their stats line by line, from a text file, and then splits the values accordingly. Everything seems to be working fine, I am even using a pretty similar piece of code in another part of the game and it works just fine, but when I try to use it here, it gives me this exception.

public static void parseBuildings()
{
    Building[] parsedBuildings = new Building[500];

    string[] buildingList = System.IO.File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\data\buildings.turd");

    int i = 0;
    foreach (string line in buildingList)
    {
        string[] parts = line.Split(',');

        //MessageBox.Show(parts[0] + parts[1] + parts[2] + parts[3] + parts[4] + parts[5]);

        //File format - name,description,unused(price),baseprice,count(unused),hps

        parsedBuildings[i].name = parts[0];
        parsedBuildings[i].description = parts[1];
        //parsedBuildings[i].price = int.Parse(parts[2]);
        parsedBuildings[i].baseprice = int.Parse(parts[3]);
        //parsedBuildings[i].count = int.Parse(parts[4]);
        parsedBuildings[i].hps = int.Parse(parts[5]);
        i++;
    }

    Array.Resize(ref parsedBuildings, buildingList.Length);

    buildings = parsedBuildings;
}

This here makes the game fill the array 'buildings' from a temporary array called 'parsedBuildings'

The funny thing is that the NullReferenceException happens at the parsedBuildings[i].description = parts[1]; line, and the commented message box works just fine.

This is how the 'buildings' array is instantiated.

public static Building[] buildings = {new Building("Loading", "Loading", 1,1,0,0)};

And this is what the Building class looks like

public class Building
{
    public string name;
    public string description;
    public int price;
    public int baseprice;
    public int count;
    public int hps;

    public Building(string nam, string desc, int pri, int bpri, int cnt, int hp)
    {
        name = nam;
        description = desc;
        price = pri;
        baseprice = bpri;
        count = cnt;
        hps = hp;
    }
}

Please help me get this solution working, because it is miles better than what I previously used.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Dankons
  • 45
  • 5
  • 1
    Please pay more attention to the formatting of your code when you post. It's really not hard to get right, but it makes *such* a difference in terms of everyone else reading it. – Jon Skeet Aug 08 '14 at 20:20
  • 4
    `parsedBuildings` is an array of 500 `null` buildings... – dee-see Aug 08 '14 at 20:21
  • What is wrong with the formatting? It looks fine on my end, with all the monospaced fonts and stuff. – Dankons Aug 08 '14 at 20:21
  • 2
    @thecoolracer _now_ it looks fine, after skeet fixed it. – Dave Zych Aug 08 '14 at 20:22
  • 2
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dee-see Aug 08 '14 at 20:22

1 Answers1

3

It is not enough just to create the array

Building[] parsedBuildings = new Building[500];

You should also initialize it. For instance:

for(int k = 0; k < parsedBuildings.Length; k++)
    parsedBuildings[k] = new Building();
AlexD
  • 32,156
  • 3
  • 71
  • 65