Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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:


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
221,296 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
221,296 Points

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

Happy coding!