Sign in to follow this  
v0dKA

[.net] Trouble creating a brush

Recommended Posts

I have a class called Region which has a member called brush of type System.Drawing.Brush. This Region class has a method called setBrush(), whose purpose is self-explanatory. However, when I try to call this method, I get this runtime error: Object reference not set to an instance of an object. Why is this? Here's a part of the class: private System.Drawing.Brush m_brush = new System.Drawing.SolidBrush( System.Drawing.Color.Blue); public void setBrush(System.Drawing.Brush b) { m_brush = b; } Here's the method call. currentRegion is a valid instance of the Region class: currentRegion.setBrush(System.Drawing.Brushes.Green); Why does it give me the error?

Share this post


Link to post
Share on other sites
probably you are getting that error because currentRegion is null. Can you show some more of the code around your call?

Share this post


Link to post
Share on other sites
Quote:
Original post by kanato
probably you are getting that error because currentRegion is null. Can you show some more of the code around your call?


Yep, it was null [embarrass]

I used it in my event handler for the paint message. I didn't quite consider that most of the time, the user is not drawing any regions (which is what my program does), so most of the time, currentRegion would be null.

Thanks for the help.

Share this post


Link to post
Share on other sites
By the way, I would say that's exactly what properties in C# are for and your setBrush would be better as public Brush Brush { set { ... } }.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this