Possible Duplicate:
Adding C# labels to a form at Runtime
I can’t figure out what is making this error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
code:
Line[] myLine = new Line[10];
int lineCount = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (checkBox1.CheckState == CheckState.Checked)
{
myLine[lineCount].setPoint(new Point(e.X, e.Y));
++pointCount;
if (pointCount == 2)
{
pointCount = 0;
++lineCount;
}
}
}
Problem is here
You need to instantiate a new
Linetype element before using it.do:
It seems that Line is a class, (reference type) if you create an array of reference type then all the elements of the array gets the default value of
null, and you can’t call a instance method onnullobject.Example from MSDN – Single Dimension Arrays