Question #44003

Hi, I want to draw more than one line in wpf in c#. I want to use array to draw the lines. How can i do it?

I used the following code. I called a method named lets_draw

private void lets_draw()
{
int i,z;
z = 40;
Line[] line = new Line[20];

for(i=1;i<=10;i++)
{
line[i].Visibility = System.Windows.Visibility.Visible;
line[i].Stroke = System.Windows.Media.Brushes.Black;
line[i].X1 = z;
line[i].X2 = z;
line[i].Y1 = 40;
line[i].Y2 = 130;
z += 10;
theGrid.Children.Add(line[i]);
}
}
But it showed the following message

"An unhandled exception of type 'System.NullReferenceException' occurred in SkeletalTracking.exe
Additional information: Object reference not set to an instance of an object."
1

Expert's answer

2014-07-08T07:09:54-0400

Answer on Question#44003- Programming, C#

1. Hi, I want to draw more than one line in wpf in c#. I want to use array to draw the lines. How can i do it?

I used the following code. I called a method named lets_draw


private void lets_draw()
{
int i,z;
z = 40;
Line[] line = new Line[20];
for(i=1;i<=10;i++)
{
line[i].Visibility = System.Windows.Visibility.Visible;
line[i].Stroke = System.Windows.Media.Brushes.Black;
line[i].X1 = z;
line[i].X2 = z;
line[i].Y1 = 40;
line[i].Y2 = 130;
z += 10;
theGrid.Children.Add(line[i]);
}
}
But it showed the following message


"An unhandled exception of type 'System.NullReferenceException' occurred in SkeletalTracking.exe Additional information: Object reference not set to an instance of an object."

Solution.

You do not initialize an object Line.

Try inserting the initialization code shown below:


Line[] line = new Line[20];
for (int i = 0; i < 20; i++)
{
line[i] = new Line();
}


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS