cp1,cp2,cp3,cp4,cp5有没有初始化,如:
cp1 = new Label();

解决方案 »

  1.   

    private System.Windows.Forms.Label cp1,cp2,cp3,cp4,cp5;
    中的变量都null
    必须初始化
      

  2.   

    初始化了:这个方法中private void InitializeComponent()
    this.cp1 = new System.Windows.Forms.Label();
    this.cp2 = new System.Windows.Forms.Label();
    this.cp3 = new System.Windows.Forms.Label();
    this.cp4 = new System.Windows.Forms.Label();
    this.cp5 = new System.Windows.Forms.Label();
    this.cp6 = new System.Windows.Forms.Label();而 cp = new Label[19];
    cp[0]=cp1;
    cp[1]=cp2;
    cp[2]=cp3;
    cp[3]=cp4;
    cp[4]=cp5;是在 InitializeComponent();方法之后调用的
      

  3.   

    我再说详细一点,首先定义变量
    private System.Windows.Forms.Label cp1,cp2,cp3,cp4,cp5;
    private System.Windows.Forms.Label[] cp;
    然后是这个类的构造函数
    InitializeComponent();
    SetArrCp();
    其中InitializeComponent();方法中有
    this.cp1 = new System.Windows.Forms.Label();
    this.cp2 = new System.Windows.Forms.Label();
    this.cp3 = new System.Windows.Forms.Label();
    this.cp4 = new System.Windows.Forms.Label();
    this.cp5 = new System.Windows.Forms.Label();
    this.cp6 = new System.Windows.Forms.Label();
    SetArrCp();方法中
    cp = new Label[19];
    cp[0]=cp1;
    cp[1]=cp2;
    cp[2]=cp3;
    cp[3]=cp4;
    cp[4]=cp5;然后我在另一个方法中这样写的
    for(int i=0;i<dmodel.mtotal;i++)
    {
    cp[i].Text = dmodel[i].PartNumber;
    }运行程序执行这个方法说未将对象的一个引用设置到实例。
      

  4.   

    的确没有啊
    cp = new Label[19];只是实例化了数组,应该还加一句
    cp[1]=new Label();
      

  5.   

    dmodel.mtotal是个什么东西。你可以看看出错是哪一行(DEBUG编译)。
      

  6.   

    dmodel是不是null?
    cp数组所有对象都初始化了吗?