I am having trouble with some WPF code. This is code I was using previously with another project and it ran fine. I have a class Dots.cs which is a dependency the project containing my main Window1.xaml.cs.
Within Dots.cs I have this function:
public void MakeDraggable(Canvas theCanvas)
{
_canvas = theCanvas;
_canvas.PreviewMouseDown += DPreviewMouseLeftButtonDown;
}
I am receiving the InnerException '{System.NullReferenceException: Object reference not set to an instance of an object. at GraphicsBook.Dot.MakeDraggable(Canvas theCanvas) in ...\Dot.cs:line 261 at GraphicsBook.Window1..ctor() in...\Window1.xaml.cs:line 47}
The call to the MakeDraggable method within my Window1.xaml.cs is:
myDots = new Dot[3];
myDots[0] = new Dot(new Point(-40, 60));
myDots[1] = new Dot(new Point(40, 60));
myDots[2] = new Dot(new Point(40, -60));
for (int i = 0; i < 3; i++)
{
myDots[i].MakeDraggable(gp);
gp.Children.Add(myDots[i]);
}
For clarity line 261 in Dot.cs is:
_canvas.PreviewMouseDown += DPreviewMouseLeftButtonDown;
And line 47 in Window1.xaml.cs is:
myDots[i].MakeDraggable(gp);