title: C# initializing object
body: <p>I know this sounds simple but I'm having issues with initializing an object. Whenever I ran my code. I kept getting the error. I need to initialize encounter on line 27. </p>
  
  <p>""An unhandled exception of type 'System.NullReferenceException' occurred in WPFBattle.exe". " </p>
  
  <p>So I asked somebody and they told me </p>
  
  <p>"You forgot to initialize encounter in MainWindow's constructor. Since you didn't initialize it but still pass it in, encounter is passed in as null, which means you can't call autobattle. Public icombat encounter is the declaration, it isn't initializing it. You initialize it like you would do any user defined object by using the new keyword along with combat's constructor." </p>
  
  <p>So I tried doing that but I keep getting errors. I'm not sure why its not working. I have attached a picture of my code and any help would be greatly appreciated</p>
  
  <p><a href="https://i.stack.imgur.com/AcYE7.jpg" rel="nofollow noreferrer">PICTURE OF MY CODE</a></p>
  
  <p><a href="https://i.stack.imgur.com/UYMIL.jpg" rel="nofollow noreferrer">PICTURE OF MY CODE 2</a></p>
  
  <pre><code>1    namespace WPFBattle
  2    {
  3    
  4    
  5     public partial class MainWindow : Window
  6        {
  7            private TextBoxStreamWriter consoleWriter;
  8            private TextBox outputField;
  9            private IList&lt;ICharacter&gt; playerParty = new List&lt;ICharacter&gt;();
  10            public ICombat encounter;
  11            private CombatThread combatThread;
  12    
  13            public MainWindow()
  14            {
  15                InitializeComponent();
  16    
  17                // Redirect console
  18                consoleWriter = new TextBoxStreamWriter(outputField);
  19                Console.SetOut(consoleWriter);
  20    
  21                //creates the two parties
  22                List&lt;ICharacter&gt; party = new List&lt;ICharacter&gt;();
  23                List&lt;ICharacter&gt; enemy = new List&lt;ICharacter&gt;();
  24    
  25    
  26    
  27                //PLACE I NEED TO INITIALIZE MY OBJECT AT 
  28    
  29    
  30                combatThread = new CombatThread(encounter);
  31                combatThread.Start();
  32            }
  33    
  34            private void textBox_TextChanged(object sender, TextChangedEventArgs e)
  35            {    
  36    
  37            }
  38    
  39    
  40        }
  41    }
  </code></pre>