0

I have a static class that looks like this:

namespace Argus
{
    static class Argus
    {
        public static List<Branch> myArgus;
    }
}

and elsewhere in my code I have this:

// Add this branch to myArgus
Argus.myArgus.Add(branch);

When I run the code, I get this error:

Object reference not set to an instance of an object.

I have verified that branch is valid (it's an object of the Branch class), and have no idea what could be wrong here. I'm trying to read branch data in from a text file.

1
  • 1
    Why would myArgus ever be non-null? Also: don't do this. Static state like this is almost always really really really bad idea (both in terms of isolation and thread-safety) Commented Mar 26, 2012 at 19:42

3 Answers 3

4

You need to instantiate it; it's default value is null otherwise:

public static List<Branch> myArgus = new List<Branch>();
Sign up to request clarification or add additional context in comments.

Comments

3

You must instansiate myArgus:

public static List<Branch> myArgus = new List<Branch>();

Comments

2

You are never allocating memory for myArgus. Of course it's null.

public static List<Branch> myArgus = new List<Branch>();

You must always make references point to allocated objects in memory, otherwise they cannot be used. Trying to invoke operations on references not pointing to allocated memory will result in NullPointerException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.