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.
""An unhandled exception of type 'System.NullReferenceException' occurred in WPFBattle.exe". "
So I asked somebody and they told me
"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."
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
1 namespace WPFBattle
2 {
3
4
5 public partial class MainWindow : Window
6 {
7 private TextBoxStreamWriter consoleWriter;
8 private TextBox outputField;
9 private IList<ICharacter> playerParty = new List<ICharacter>();
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<ICharacter> party = new List<ICharacter>();
23 List<ICharacter> enemy = new List<ICharacter>();
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 }