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.

C# C# Objects Encapsulation and Arrays Null

Grigor Samvelian
Grigor Samvelian
2,852 Points

Don't understand why System.NullReferenceException will get thrown

What will happen when the following code runs?

Map map; int area = map.Width * map.Height;

could someone please explain why the NullReferenceException gets thrown here?

1 Answer

John Lack-Wilson
John Lack-Wilson
8,181 Points

Null Reference Exception is thrown when you are attempting to use something that is null.

It seems to me that you are trying to assign area with values that do not yet exist because you have not assigned Map map to anything.

You should assign Map map to something.

Grigor Samvelian
Grigor Samvelian
2,852 Points

ah ok. Wasn't coding just doing a quiz and got held up on this question. Thank you you really cleared things up.