Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:36:18+00:00 2026-06-07T00:36:18+00:00

I had a weird issue arise after using CodeMaid to clean up my code.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:36:20+00:00Added an answer on June 7, 2026 at 12:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a weird problem, where I opened up a source code project for
I had a weird problem here. Please check the code below. I do not
I'm running a weird issue here. I have code that makes jquery ajax calls
I've run into a weird issue that has had me scratching my head for
Had this weird bug in my code and I was not able to solve
I had a weird issue with IE8 , actually my friend who was testing
I came across a very weird issue where in my querystirng had ++ as
I'm using Heroku for the first time and am having a weird issue. I've
I have a really weird issue here. I'm using my local development server right
I've been having this weird issue with jQuery which came with Wordpress 3.2.1. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.