0

I have a static class like this:

  public static class ApplicationList
    {
        public static List<ApplicationsModel> ApplicationsModels { get; set; }
    }

I want to use ApplicationsModels in another class

   class Program
    {
        static void Main(string[] args)
        {
            GetApplicationNameFromAppConfigAndAddToApplicationList();
        }

        private static void GetApplicationNameFromAppConfigAndAddToApplicationList()
        {
            List<string> applicationName = ConfigurationManager.AppSettings["AppName"].Split(',').ToList();

            foreach (var variable in applicationName)
            {
                ApplicationList.ApplicationsModels.Add(new ApplicationsModel { DateTime = DateTime.MinValue, Name = variable });

            }
        }
    }

But i my ApplicationList is null and get this error

Object reference not set to an instance of an object.

This is static Prpoerty and we can't create instance form static Prpoerty

enter image description here

3 Answers 3

4

Well you have a

 public static List<ApplicationsModel> ApplicationsModels { get; set; }

property which is not initialized.

Can do something like this, instead:

public static class ApplicationList
{
    private static List<ApplicationsModel> appmodel = new List<ApplicationsModel>();
    public static List<ApplicationsModel> ApplicationsModels 
    { 
       get { return appmodel ;}
    }

    //DON'T THINK YOU NEED A SET IN THIS CASE.. 
    // BUT ADD IT, IF NEED
}
Sign up to request clarification or add additional context in comments.

2 Comments

should static member initialize?
Sure. Static is access definition, but it remains a simple member like any other possible member, if you not initialize it, the default value for specified type will be applied. For List<T>, which is a reference type the default value is null.
2

You get the null reference exception because the List<ApplicationsModel> ApplicationsModels is still null when you try to use it. Just declaring it as static doesn't means that the compiler or the framework adds for you the call to

ApplicationsModels = new List<ApplicationsModel>();

Comments

1

Short answer, add this line before loading your list:

ApplicationsModels = new List<ApplicationsModel>();

Longer answer:

You need to initialise your List before you can add to it.

This is because a (reference-type) property will be null by default, and it wouldn't make sense to do null.Add(item).

Solution 1: Initialise before loading:

The easy solution is to add an initialisation line before loading your list in your Get... method:

ApplicationsModels = new List<ApplicationsModel>();

However, this will reinitialise the list each time you call the Get method (which may or may not be what you want). You could place a check to only recreate the list if it is null, but if that's the behaviour you're after I'd lean towards one of the other solutions below.

Solution 2: Use an initialised field instead of a property:

Another way is to replace your property with an initialised field. Either:

public static List<ApplicationsModel> ApplicationsModels = new List<ApplicationsModel>();

Or:

public static List<ApplicationsModel> ApplicationsModels 
{ 
    get { return _applicationsModels; }
    set { _applicationModels = value; } // Do you really want a set?
}
private static List<ApplicationsModel> _applicationsModels = new List<ApplicationsModel>();

Solution 3: Keep a property, but use a static constructor:

Another way is to use a static constructor in ApplicationList:

static ApplicationList()
{
    ApplicationsModels = new List<ApplicationsModel>();
}

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.