1

I am getting the error in the title when I run this code during the Add method. The Add method should add the gameobject to a list called queue.
GameObject is a class.
GameManager is a class as well.
queue is a list.

I think this is the only code relevant.

    static void Main()
    {
        GameObject obj1 = new GameObject();
        GameManager manager1 = new GameManager();
        obj1.name = "First";
        manager1.Add(obj1);
        manager1.Process();
    }

    public void Add(GameObject gameObject)
    {
        gameObject.initialize = true;
        queue.Add(gameObject);
    }
MattDavey
  • 8,897
  • 3
  • 31
  • 54
M K
  • 13
  • 1
  • 1
  • 3

3 Answers3

3

Try initializing the field "queue":

  • It's probably never initialized, hence the Object Reference not set to an instance of an object error error, which by the structure of the Add method is likely to correspond to queue.

  • Also - I would consider renaming queue to a meaningful name, since it is currently deceiving.

Here is an initialization suggestion, it is for a List as you described in your question, but can easily be modified to correspond to any IEnumerable , also, it can be done in a different method being executed prior to Main (again, by the looks of your code it seems unlikely) :

private List<GameObject> queue; // assuming it's private, doesn't really matter either way.

static void Main()
{
    queue = new List<GameObject>(); // the missing line
    GameObject obj1 = new GameObject();
    GameManager manager1 = new GameManager();
    obj1.name = "First";
    manager1.Add(obj1);
    manager1.Process();
}


public void Add(GameObject gameObject)
{
    gameObject.initialize = true;
    queue.Add(gameObject);
}
Ron.B.I
  • 2,726
  • 1
  • 20
  • 27
1

You have to initialize Queue list first

0

You need to debug your code.

I assume you are using Visual Studio, if so then do this:

  1. Go to the Debug menu.

  2. Click on the the Exceptions... choice.

The following dialog should appear:

enter image description here

Note: the Common Language Runtime Exceptions checkbox is checked.

Upon clicking OK, now when you debug your code anytime an exception is thrown by your code or the .NET Framework, the debugger will halt on the line that threw the exception. This makes finding where something is "breaking" much easier.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • This would only really help for exceptions which are caught/handled/swallowed. For an unhandled exception like the one the OP described, the debugger is going to break anyway. – MattDavey Jul 10 '13 at 08:30
  • @MattDavey - No, this will break as soon as an exception is thrown; not when it bubbles all the way up the chain. – Karl Anderson Jul 10 '13 at 11:45
  • I understand that, but for unhandled exceptions you're not going to see any difference in behaviour by doing this. The debugger is still going to break on the line that threw the exception regardless. – MattDavey Jul 10 '13 at 12:08