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?
globalidis not getting reached is because you are never creating an instance ofGlobalClass, so whenEntity()is trying to getglobalidit crashes.