Label[] label = new Label[12];
        public light()
        {
            InitializeComponent();
            label[0].Location = new Point(120, 150);
            label[1].Location = new Point(138, 150);
            label[2].Location = new Point(156, 150);
            label[3].Location = new Point(174, 150);
            label[4].Location = new Point(174, 132);
            label[5].Location = new Point(174, 114);
            label[6].Location = new Point(174, 96);
            label[7].Location = new Point(156, 96);
            label[8].Location = new Point(138, 96);
            label[9].Location = new Point(120, 96);
            label[10].Location = new Point(120, 114);
            label[11].Location = new Point(120, 132);
            for (int i = 0; i < label.Length;i++ )
            {
                label[i].Size = new Size(16, 16);
                label[i].BackColor = Color.Blue;
                label[i].Text = "";
                panel1.Controls.Add(label[i]);
            }
        }其中的Label[] label = new Label[12];
是类中字段声明,light()是构造函数,整个程序运行之后没有出现界面,调试时单步运行完label[0].Location = new Point(120, 150);这句代码后就出现异常,说“未将对象引用设置到对象的实例。”这是何解?

解决方案 »

  1.   

    你的lable都没得实例化,哪儿来的Location 
    加 label[0]=new Label();
    label[0].Location = new Point(120, 150);
      

  2.   

    Label[] label = new Label[12];
    没有初始化,只是声明而已。public light()
      {
      InitializeComponent();
      label[0] = new Label();
      label[0].Location = new Point(120, 150);
      label[1] = new Label();
      label[1].Location = new Point(138, 150);
    }另一句,写代码的方式我不提,只说一点,学会自己调试,别只会把错误信息读出来。编译器做了那么强大的调试功能你却连最简单的使用的不会,这真是一种悲哀,你所看的错误信息一个丝毫不懂编程的人一样可以读到。
      

  3.   


    //Label[] label = new Label[12];
    //省略前面
    //InitializeComponent();
    for (int i = 0; i < label.Length; i++)
    {
         label[i] = new Label();
    }
    //省略后面