Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

System.NullReferenceException, filename unknown (Tower Defense project)

Hello all — I'm getting the following error when running my code:

Unhandled exception reads: System.NullReferenceException: Object reference not set to an instance of an object at TreehouseDefence.Game.Main () <0x4087fd50 + 0x0041b> in <filename unknown>:0

It compiles just fine. Here's a snapshot:

https://w.trhou.se/kfaazdqhe8

I guess I have two questions:

1) Why am I not getting details on where this exception is stemming from? Even if I run the compiler using -debug I don't get any extra details. All I can work out is that it's coming from somewhere within the try ... catch block in my Main() method.

2) What part did I mess up?? Hah.

1 Answer

Steven Parker
Steven Parker
231,236 Points

I was able to replicate your message by running "Game.exe", but I noticed that it had been built before the last edit to "Game.cs".

So I rebuilt the project and when I run that, I get no error. So it seems you'd already fixed the issue in the source but just had not rebuilt yet.

You won't believe it...

I've been running this to compile for a while:

clear && mcs -out:Game.exe *.cs && mono Game.exe

But then I got lazy and started running it as:

clear && mcs *.cs && mono Game.exe

...little realising that we'd subsequently added Exceptions.cs in the videos and it was now compiling as Exceptions.exe... :)

Oops. Thanks for taking the time to look, now I'll know to check that!

Steven Parker
Steven Parker
231,236 Points

I just figured that other executable was from some extra-curricular experimentation. :wink:

Happy coding!