Why is my event not set to an instance of an object?

I am trying to create an event in a child class ( A polygon class i wrote myself, that allows you to rotate, move, check if it contains a point etc ) that fires whenever the move class is called, and is handled by its parent class ( Wich is essentially a collection, i want it to move all of the items it contains when it's own polygon class is moved )

However when i try to run the code. I get an error saying the e_Moved object is null. Any help understanding this would be great

Thx in advance
// PolygonF.cs
namespace PlacementManager
{
    public delegate void HasMoved(SizeF offset);
 
    public class PolygonF
    {
        // Events
        public event HasMoved e_Moved;
 
// ... the rest of the class, no referance to any events ...
 
        private void MoveBy(PointF p)
        {
            for (int i = 0; i < m_int_Pointer; i++)
            {
                m_pnt_Points[i].X += p.X;
                m_pnt_Points[i].Y += p.Y;
            }
            SetUpVertices();
            SetUpBounds();
            // This is where i get the error:
            // Object (e_Moved) is null
            e_Moved.Invoke(new SizeF(p));
        }       
 
// ... rest of the class ...
    }
}
 
 
 
// ParentClass
namespace PlacementManager
{
    public class Zone : System.Collections.CollectionBase, Selected
    {
 
        // Other Code
        private PolygonF m_pol_Shape;
        private Int32 m_int_ID;
        private String m_str_Name;
        public Zone(Int32 ID,String name, RectangleF shape, float rotation, RectangleF ScreenSize, RectangleF ZoomRect, Color c)
        {
            m_int_ID = ID;
            m_str_Name = name;
            m_pol_Shape = new PolygonF(shape.X, shape.Y, shape.Height, shape.Width, rotation, ZoomRect, ScreenSize, false);
            m_pol_Shape.FillColor = c;
            m_pol_Shape.Filled = true;
 
 
            // Relevent
            m_pol_Shape.e_Moved += new HasMoved(thisMoved);
        } 
 
        public void thisMoved(SizeF offset)
        {
            foreach (Placement p in List)
            {
                p.ForceMove(offset);
            }
        }
 
// ... rest of the class ...
   }
}

Open in new window

ClearRockAsked:
Who is Participating?
 
p_davisConnect With a Mentor Commented:
can you put a break point to confirm that

m_pol_Shape.e_Moved += new HasMoved(thisMoved);

is being hit?

make sure you're calling the correct constructor for the zone class.
0
 
CuteBugCommented:
It is apparent that before the code

m_pol_Shape.e_Moved += new HasMoved(thisMoved);

is executed, the MoveBy() method is being called which in turn is firing the event.
The event, being null, is causing the exception to be raised.

Do the following in MoveBy() method


private void MoveBy(PointF p)
        {
            for (int i = 0; i < m_int_Pointer; i++)
            {
                m_pnt_Points[i].X += p.X;
                m_pnt_Points[i].Y += p.Y;
            }
            SetUpVertices();
            SetUpBounds();
            if (e_Moved != null)
                e_Moved.Invoke(new SizeF(p));
        } 

Open in new window

0
 
p_davisCommented:
just check for e_moved being null

if(e_moved != null)
       e_Moved.Invoke(new SizeF(p));
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
p_davisCommented:
darn
0
 
CuteBugCommented:
:)
0
 
ClearRockAuthor Commented:
I had originally tried the following code, wich does avoid the error, but the event is always equal to null, and so the delegate ( or the handler, not sure of the correct termanology here ) is never called...

I'm a java programmer primarily so this is probebly just a case of me not understanding the c# event handleing procedures
private void MoveBy(PointF p)
        {
            for (int i = 0; i < m_int_Pointer; i++)
            {
                m_pnt_Points[i].X += p.X;
                m_pnt_Points[i].Y += p.Y;
            }
            SetUpVertices();
            SetUpBounds();
            if (e_Moved != null)
            {
                e_Moved.Invoke(new SizeF(p));
            }
        }

Open in new window

0
 
ClearRockAuthor Commented:
To expand on my original question wich clearly wasn't that clear, the problem is actually getting the event to fire, not dealing with the null ref error itself. Sorry for the poorly worded question
0
 
ClearRockAuthor Commented:
To expand on my original question wich clearly wasn't that clear, the problem is actually getting the event to fire, not dealing with the null ref error itself. Sorry for the poorly worded question
0
 
ClearRockAuthor Commented:
To expand on my original question wich clearly wasn't that clear, the problem is actually getting the event to fire, not dealing with the null ref error itself. Sorry for the poorly worded question
0
 
p_davisCommented:
have you tried to put your delegate declaration within the class?

public class PolygonF
    {

 public delegate void HasMoved(SizeF offset);
0
 
p_davisCommented:
disregard my last post -- that shouldn't matter.
0
 
p_davisCommented:
where is your instantiation of the Zone class -- you have to instantiate it to get that constructor that contains the hook for the event to run
0
 
ClearRockAuthor Commented:
If By instantiation you mean where is it constructed, in a class mile above that loads the whole class tree from a database. I've attached the method. ( Its loading a combination of test data ( for the site and plan classes) and real data ( for the zone and placement classes )

Sorry for the multiple posts, my internet is playing up atm
private void cmdPopulate_Click(object sender, EventArgs e)
        {
 
            CCOLGroups gps = new CCOLGroups();
            CCOLContainerPlacement cpls = new CCOLContainerPlacement();
 
            PointF[] pos = new PointF[6];
            pos[0] = new PointF(0,0);
            pos[1] = new PointF(2000, 1000);
            pos[2] = new PointF(3000, 1500);
            pos[3] = new PointF(3000, 2500);
            pos[4] = new PointF(2000, 3000);
            pos[5] = new PointF(1000, 2500);
            //m_obj_Plan.AddSite("Plunder n' Bootie", p, Color.Aquamarine);
            Int32[] pIDs = { 0, 1, 2, 3, 4, 5 };
 
 
            Site s = m_obj_Plan.AddSite(1,"Plunder n' Bootie", pos, pIDs, Color.Aquamarine);
 
            gps.ReadGroups();
            //gps.GetGroup(1)
            //SqlDataAdapter drGroups = gps.GetGroups();
 
            foreach (CGroupItem gp in gps)
            {
                //Int32 lGroupID = m_obj_Plan.Key;
 
                PointF[] poz = new PointF[4];
                poz[0] = new PointF(gp.LocationX, gp.LocationY);
                poz[1] = new PointF(gp.LocationX + gp.SizeX, gp.LocationY);
                poz[2] = new PointF(gp.LocationX + gp.SizeX, gp.LocationY + gp.SizeY);
                poz[3] = new PointF(gp.LocationX, gp.LocationY + gp.SizeY);
 
                Int32[] pIDz = { 0, 1, 2, 3 };
                Zone z = s.AddZone(Int32.Parse(gp.Key), gp.Description, poz, pIDz, Color.FromArgb(Convert.ToInt32(gp.FillColour)));
 
                //Zone z = s.AddZone(737, "jafskjasfkjafs", new RectangleF(900, 900, 800, 800), 0, Color.Blue);
 
                cpls.ReadDatabase(Convert.ToInt32(gp.Key));
 
                foreach (CContainerPlacement cplc in cpls)
                {
 
                    Placement p1 = z.AddPlacement((Int32)cplc.Key, cplc.Detail, cplc.Position_X, cplc.Position_Y,
                                                    cplc.Width, cplc.Length, (cplc.Rotation / 180f) * (float)Math.PI, Color.FromArgb(Convert.ToInt32(cplc.Colour)));
                }
            }
            objPlacementManager.Refresh();
            //Placement p1 = z.AddPlacement(123, "lala", 1000, 1000, 50, 200, 0, Color.OrangeRed);
            
            
        }

Open in new window

0
 
ClearRockAuthor Commented:
Hahaha, sorry. Disregard that your right i have a second overloaded constructor that isn't adding the delagate or handler or whatever.

I can't belive i didn't see that... just testing it now works
0
 
p_davisCommented:
the zone class object only has scope while this method is active. well, from what i can tell anyway, which wouldn't fire any events outside of this method.
0
 
p_davisCommented:
well then my assumption in my last post wasn't correct.--
glad you got it now.
0
 
ClearRockAuthor Commented:
once i copyed the assignments into my second constructor it all worked fine. Thx a lot for the help, what a stupid mistake
0
 
p_davisCommented:
it's easy to overlook anything.

thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.