Null reference exception when using lists of my object.

This is a huge head scratcher (at least, it has been for me). I literally have only one problematic line of code I've written, that checks out in Visual Studio, but starting my game immediately boots me out with a null reference exception error based on that single line no matter how I jiggle it around or configure it or rephrase it. I have a lot of experience with collections of objects in VB, and as far as I can tell, I'm not doing anything wrong, but I've never used collections of objects in C#, so.

Here is my code.

using UnityEngine;
using System.Collections;

public class CountInventory : MonoBehaviour {

    // Use this for initialization
    public static int CurrentWater = 50;
    public static int CurrentCash = 100;
    public static int ToolMode = 1;
    public static System.Collections.Generic.List<Seed> SeedBox;
    private Seed NewSeed;
    void Start ()
    {
        SeedBox.Add(NewSeed);
    }

    // Update is called once per frame
    void Update ()
    {
        GameObject.Find("GUIWater").guiText.text = "Water: "+CurrentWater;
        GameObject.Find("GUIMoney").guiText.text = "$"+CurrentCash;
    }
}

Now, another thing to note, before someone brings it up: I do have a Seed class, so I'm not trying to declare a type that doesn't exist. :) You may also ignore my Update, and Water/Cash/Toolmode variables. I just included everything for posterity.

Any time I try to do ANTHING with my SeedBox (which is declared as a list of Seeds, or it logically seems that way to me), it gives me null reference exceptions up the wazoo. I've tried all sorts of alternate codes in place of `SeedBox.Add(NewSeed);`. I've tried `SeedBox.Add(new Seed());`, I've tried directly talking to the index (by using `SeedBox[0].SeedName` or whichever other Seed variable)... nothing works.

Maybe I'm just not understanding how lists work in C#. Anyone have a good answer for this one?

You need to create the list before using it.

using System.Collections.Generic;
...
public static List<Seed> SeedBox = new List<Seed>();

The reason you're getting null reference exception is because SeedBox always was null for you.

This has nothing to do with lists, collections or arrays. This is C# basics. All reference type objects are null by default. Unity create instances for you when you have public (not static) variables on scripts, so if you haven’t coded C# outside of unity I can imagine you are having problems wrapping your head around it. :slight_smile: But this is nothing more than you hadn’t created the list in the first place.


On a completely different note; you probably shouldn't call GameObject.Find every update. You can store the references during start instead:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CountInventory : MonoBehaviour 
{
    public static int CurrentWater = 50;
    public static int CurrentCash = 100;
    public static int ToolMode = 1;
    public static List<Seed> SeedBox = new List<Seed>();

    private Seed NewSeed;
    private GUIText water;
    private GUIText money;

    void Start ()
    {
        water = GameObject.Find("GUIWater").guiText;
        money = GameObject.Find("GUIMoney").guiText;
        SeedBox.Add(NewSeed);
    }

    void Update ()
    {
        water.text = "Water: " + CurrentWater;
        money.text = "$" + CurrentCash;
    }
}