简体   繁体   English

C#中的折线数组无法运行

[英]polyline array in c# won't run

i am trying to create a polyline array so that the end user can add as many polylines to his canvas as he wants to, the code won't run and wile debugging it writes: "Exception has been thrown by the target of an invocation." 我试图创建一个折线数组,以便最终用户可以向画布中添加任意多条折线,该代码将无法运行,并且不会对其编写的调试进行调试:“调用的目标引发了异常。 ” here is a part of the code 这是代码的一部分

Polyline[] linije = new Polyline[10];        
linije[0].Stroke = System.Windows.Media.Brushes.Black;           
linije[0].StrokeThickness = 1;        
linije[0].Points = poli.Points;          
canvas1.Children.Add(linije[0]);

You have a null reference exception because you don't create a Polyline instance in element 0 of your array. 您有一个null引用异常,因为您没有在数组的元素0中创建Polyline实例。

Polyline[] linije = new Polyline[10];     
linije[0] = new Polyline();  // Create the Polyline object!!!
linije[0].Stroke = System.Windows.Media.Brushes.Black;           
linije[0].StrokeThickness = 1;        
linije[0].Points = poli.Points;          
canvas1.Children.Add(linije[0]);

Remember that creating an array of a certain type DOESN'T create the objects for each element in that array. 请记住,创建某种类型的数组不会为该数组中的每个元素创建对象。 You can easily spot that by using the debugger . 您可以使用调试器轻松发现这一点。 If you add a breakpoint just after after the line Polyline[] linije = new Polyline[10] and you add a watch for linije[0] you'll see that it is null. 如果在Polyline[] linije = new Polyline[10]行之后添加断点,并添加监视linije [0]的值,您会发现它为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM