0

I'm trying to create an instance of a quest and add it to array using singleton pattern. When I run my program and it goes through this code, it stops and throws NullReferenceException. What may cause the problem?

public class IntroductionQuest : Quest
    {
        //SINGLTON REALIZATION
        protected static IntroductionQuest instance;
        private IntroductionQuest(string Name) { name = Name; }
        public static IntroductionQuest Initialize(string Name)
        {
            if (instance == null)
            {
                instance = new IntroductionQuest(Name);
                PublicData.Quests.Add(instance);
            }
            return instance;
        }
        //SINGLETON REALIZATION
    }

1 Answers1

0

assuming name is defined in a base class, try this

if(PublictData!=null)
{
if  (PublicData.Quests==null) PublicData.Quests=new List<IntroductionQuest>();

PublicData.Quests.Add(instance);

}
Serge
  • 40,935
  • 4
  • 18
  • 45