代码如下,我使用了静态的类,MyLabel.MyLabelBelt是我自定义的控件,继承自labelnamespace CarouselManager
{
    public static class testlabel
    {
        public static MyLabel.MyLabelBelt[] LabelBelt = new MyLabel.MyLabelBelt[1000];
    }
    public partial class View_Belt_Dyna 
    {
                public View_Belt_Dyna()
        {
            InitializeComponent();
            for (int i = 0; i < 1000; i++)
            {
                testlabel.LabelBelt[i] = new MyLabel.MyLabelBelt();
            }
            
        }        private void simpleButton_Init_Click(object sender, EventArgs e)
        {
            for (int ii = 0; ii < 1000; ii++)
            {
                if (testlabel.LabelBelt[ii].Area.Trim() == "仓库1号")
                {
                    .........
                }
             }
        }
  
     }}
编译可以通过,问题是,运行时停在这句上:if (testlabel.LabelBelt[ii].Area.Trim() == "仓库1号")提示:未将对象引用设置到对象的实例。使用new关键字创建对象实例。我就奇怪了,我已经在构造函数里面把所有数组成员都new了啊,怎么到这个simpleButton_Init_Click按钮响应函数里面又要再new呢?求解答!谢谢!

解决方案 »

  1.   

    如果构造函数执行了,其他地方又没有清空静态数组里面的对象的话,应该指的是Area为null吧
      

  2.   

    你调试了吗?哪个是null?是testlabel.LabelBelt[ii]?还是testlabel.LabelBelt[ii].Area?
      

  3.   

    Area是一个自定义的属性,string类型的,难道这个属性也要New吗?
      

  4.   

    假如你自定义属性的时候初始化成null了呢,调试一下不就看到哪个是null了
      

  5.   

    谢谢大侠们,加了这句好了:
    if (testlabel.LabelBelt[ii].Area != null)