-1

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

PICTURE OF MY CODE

PICTURE OF MY CODE 2

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    }
David Rawson
  • 20,912
  • 7
  • 88
  • 124
firmfiasco
  • 121
  • 2
  • 2
  • 9

2 Answers2

1

At line 10: You have the following code:

public ICombat encounter;

But this variable encounter never becomes a value. So it's null. On line 30: You pass this null value to your combatThread.

combatThread = new CombatThread(encounter);

If combatThread access encounter somewhere like:

encounter.DoSth();

It will throw a

NullReferenceException

Because encounter is of type ICombat and this is an Interface, you can't use the new operator on it. You will need a class which implements this interface.

public class Combat : ICombat
{
   //Implement what the Interface need
}

And create a new object of this for your encounter reference.

encounter = new Combat(); //IMPORTANT
combatThread = new CombatThread(encounter);
combatThread.Start();
Sebi
  • 3,879
  • 2
  • 35
  • 62
0

To initialize encounter you need to call

public class Combat : ICombat
{
   //Combat class
}

encounter = new Combat()//and of course provide necessary parameters if needed

Though since you've already tried that I'd guess the problem resides somewhere else. How do you use encounter in CombatThread? I'd guess you keep a reference to it in CombatThread but have forgotten or made a faulty assignment of CombatThread.mEncounter.

In CombatThread how do you initialize the reference to encounter?

Sebi
  • 3,879
  • 2
  • 35
  • 62
user3532232
  • 257
  • 8
  • 19
  • Thank you for your response. In the first picture attachment I included my CombatThread code. – firmfiasco Nov 25 '16 at 06:41
  • If you place a breakpoint on CombatThread.Thread.Start(), is encounter really null? What happens in Autobattle()? Perhaps the null reference is thrown from there? – user3532232 Nov 25 '16 at 06:48
  • I added a breakpoint on line 30 and it works fine but when I continue to line 31 it then throws the null refrence. – firmfiasco Nov 25 '16 at 06:57
  • In the pictures you've provided I can't see a encounter = new Combat() call. Have you added this? – user3532232 Nov 25 '16 at 07:05