[.net] Trouble creating a brush

Started by
2 comments, last by Bob Janova 17 years, 6 months ago
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?
.:<<-v0d[KA]->>:.
Advertisement
probably you are getting that error because currentRegion is null. Can you show some more of the code around your call?
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.
.:<<-v0d[KA]->>:.
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 { ... } }.

This topic is closed to new replies.

Advertisement