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

  • Home
  • SEARCH
  • 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 9258973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:32:34+00:00 2026-06-18T12:32:34+00:00

public class MyClass { // private fields ////////////////////////////////////////////////////////////////////////// public MyClass(string param1, string param2) {

  • 0
public class MyClass
{
    // private fields


    //////////////////////////////////////////////////////////////////////////
    public MyClass(string param1, string param2)
    {
        // do some stuff
    }

    private static object syncRoot = new Object();
    private static volatile MyClass instance = null;

    public static MyClass  Log
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new MyClass();
                }
            }

            return instance;
        }
    }

    private MyClass()
    {
        // do some stuff
    }

    public void myFunction(string txt, uint flags)
    {
         // do some stuff
    }
}

This is my class and I use it this way

    MyClass.Log.myFunction("some string", flags);

But constantly get that either MyClass is null or MyClass.Log is null when i use this class in other class functions.

What do I do wrong?

In addition to the question here is the error i get:

 System.NullReferenceException: Object reference not set to an instance of an object.
   at MyNamespace.MyClass..ctor()
   at MyNamespace.MyClass.get_Log()
  • 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-18T12:32:35+00:00Added an answer on June 18, 2026 at 12:32 pm

    The code you showed looks good.
    MyClass can’t be null as it is a class. Log shouldn’t be null either, your singleton implementation looks OK.

    My guess is that the problem is that you are using the parameterless constructor of MyClass. I guess that myFunction somehow uses something that is only initialized in the constructor that takes the two parameters.

    Actually, according to your stack trace the problem is inside the parameterless constructor. I guess you are trying to log something in there, like this : instance.Log(...);. That won’t work, because at that point instance is still null. You should simply use Log(...) instead.

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

Sidebar

Related Questions

I have a private static readonly field in my class: public class MyClass {
This isn't valid code: public class MyClass { private static boolean yesNo = false;
I have a class like this: private class MyClass { [DisplayName(Foo/Bar)] public string FooBar
I have class @Embeddable public class MyClass implements Serializable { private String field1; private
public class MyClass { private String s = foo; } Is it possible to
I have a class that looks like the following: public class MyClass { private
In a class, I have a container: public class MyClass implements MyClassInterface { private
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a List<MyClass> The class is like this: private class MyClass { public
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <

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.