2

I had a weird issue arise after using CodeMaid to clean up my code. Now, the class which holds all my global variables and functions is throwing exceptions and I can't figure out why.

The outer exception is thrown in GlobalClass.GetID(): TypeInitializationException.

The inner exception is: Object reference not set to an instance of an object

Here's an example of some code that is causing this.

The library

namespace ErrorCode //Library
{

    public static class GlobalClass
    {
        private static int _globalid = 0; //Never reached
        public static int GlobalID
        {
            get
            {
                return _globalid;
            }
        } //Read-Only

        public static int GetID()
        {
            retun _globalid++; //Crashes here with TypeInitialzationException
        }
    }

    public class Entity
    {
        private int _id;
        public int ID
        {
            get
            {
                return _id;
            }
        }
        public Entity()
        {
            _id = GlobalClass.GetID(); //Crashes here with object reference not set to an instance of an object? 
        }
    }
}

The actual program

using ErrorCode;
namespace MainProgram //The program that will run
{
    public class Program
    {
        public Entity e = new Entity(); //Triggers GlobalClass.GetID()
    }
}

Any ideas?

6
  • The reason globalid is not getting reached is because you are never creating an instance of GlobalClass, so when Entity() is trying to get globalid it crashes. Commented Jul 4, 2012 at 3:16
  • @MatthewRz no, GlobalClass is static. Commented Jul 4, 2012 at 3:18
  • @Blorgbeard Hmm, that's what I thought. Commented Jul 4, 2012 at 3:20
  • 2
    What other static initializers do you have in your GlobalClass? Commented Jul 4, 2012 at 3:21
  • 1
    This code works for me.. Maybe you lost the bad code when simplifying it for SO Commented Jul 4, 2012 at 3:22

3 Answers 3

1

You edited your code down too much and removed the real cause of the exception. A class with a field initializer like this:

public static class Globals {
    private static int _globalid = 0;
}

is not directly supported by the CLR. The compiler rewrites this code, it creates a static constructor for the class (or modifies an existing one) and writes it like this instead:

public static class Globals {
    private static int _globalid;
    static Globals() {
        _globalid = 0;
    }
}

It does this for all the static fields with an initializer. One of which is throwing the exception in your case, we can't see it in your snippet. One way to chase it down is to force the debugger to stop on the exception, Debug + Exceptions, tick the Thrown checkbox for CLR exceptions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I'll give it a try! Seems like it would fix it!
1

Try using a static constructor:

public static class GlobalClass
{
    static GlobalClass()
    {
        GlobalClass._globalid = 0;
    }

    private static int _globalid;
    public static int GlobalID
    {
        get
        {
            return _globalid;
        }
    }

    //...
}

3 Comments

Wouldn't a constructor added to a static class never be called since we never initialize a new object off of GlobalClass?
It's a "static" constructor, which gets called "like" a constructor. But this will reveal the error if not fix it. We've run into concurrency problems before where we had to explicitly specify the static constructor, rather than relying on the compiler to do it for us.
Oh ok, I didn't know that static classes could have a constructor, this'll simplify a lot in my code.
0

I suspect, as Anthony Pegram implies, that you have a static constructor or another static field initializer that you haven't included in your sample code. This constructor or initializer is throwing the null reference exception.

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.