none
为什么在public Form1()内创建的实例在同一窗体别的方法为null?

    问题

  • 我用VS2015创建了一个windows窗体程序(请问这个是不是叫winfrom?),在

    public partial class Form1 : Form下声明了一个变量a,然后在

    public Form1下创建一个实例,并用a引用,然后在

    private void button_Click方法下调用a,运行时返回了一个a的值为null的故障

    按道理来说,Form1类之内建立的a的实例是public的,那即使button_Click方法是private的也能读取到a的实例的啊,为什么a的值会是null呢?

    另外,我在buttonclick方法里建立a的实例就不会报错顺利运行,这个好解释。

    namespace DogBet
    {
        public partial class Form1 : Form
        {
            Greyhound[] Playdog;}
            
            
            public Form1()
            {
                Playdog = new Greyhound[4];
                Playdog[0] = new Playdog();}
    
             private void racebutton_Click(object sender, EventArgs e)
             {
             Anotherdog=Playdog[0]
             //这里运行时会产生Playdog[0]为null的错误,代码可能有地方有错误,但是
              //基本意思就是这样
              }


    • 已编辑 Mirage_j 2016年4月8日 6:49
    2016年4月8日 6:47

答案

  • Hi Mirage_j,

    我推测你出错可能和构造函数那边有关,在未完成界面初始化前你就调用了界面上的控件(未初始化前应该只是一些存放了null值的变量),你可以尝试把那段代码放到 InitializeComponent()后面。

    范例:

            public Form1()
            {
                InitializeComponent();   
                //滞后到控件初始化完成后赋值    
                Playdog = new Greyhound[4];
                Dogbox = new PictureBox[4];
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i] = new Greyhound();
                    Playdog[i].MyPicturebox = new PictureBox();
                    Dogbox[i] = new PictureBox();
                }
                Dogbox[0] = DogBox1;
                Dogbox[1] = DogBox2;
                Dogbox[2] = DogBox3;
                Dogbox[3] = DogBox4;
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i].MyPicturebox = Dogbox[i];
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    2016年4月18日 6:40

全部回复

  • 断点调试

    还有,你应该把你如何构造Form1的,是否还有其他构造函数?

    你的Playdog是怎么样的告诉我们,但是你的这个代码,我用最简单的类来替换掉 Playdog 运行是没有问题的。

      class A{}
      public partial class Form1 : Form
      {
        private A[] a;
        public Form1()
        {
          a = new A[4];
          a[0] = new A();
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          A b = a[0];
        }
      }


    Bob Bao

    Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
    你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。

    2016年4月8日 8:02
  • 好的,我的Greyhound类里有个Mypicturebox的picturebox字段。而我将playdog数组里各项的Mypicturebox引用了我窗体上建立的四个叫dogbox的控件,想利用playdog来改变dogbox在窗体中的位置,具体是这样的
    //这是greyhound类的内容
    namespace DogBet
    {
        class Greyhound
        {
            public int RacetrackLength;
            public PictureBox MyPicturebox;}

    //这是Form1.cs类的内容
    namespace DogBet
    {
        public partial class Form1 : Form
        {
            Greyhound[] Playdog;
            PictureBox[] Dogbox;
            
            public Form1()
            {
                Playdog = new Greyhound[4];
                Dogbox = new PictureBox[4];
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i] = new Greyhound();
                    Playdog[i].MyPicturebox = new PictureBox();
                    Dogbox[i] = new PictureBox();
                }
                Dogbox[0] = DogBox1;
                Dogbox[1] = DogBox2;
                Dogbox[2] = DogBox3;
                Dogbox[3] = DogBox4;
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i].MyPicturebox = Dogbox[i];
                }
                InitializeComponent();          
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
    
            private void racebutton_Click(object sender, EventArgs e)
            {
                //如果将以下注释掉的部分取消注释就能顺利运行
                //达到我要的效果
                //for (int i = 0; i < 4; i++)
                //{
                //    Playdog[i] = new Greyhound();
                //    Playdog[i].MyPicturebox = new PictureBox();
                //    Dogbox[i] = new PictureBox();
               // }
               // Dogbox[0] = DogBox1;
               // Dogbox[1] = DogBox2;
               // Dogbox[2] = DogBox3;
                //Dogbox[3] = DogBox4;
                //for (int i = 0; i < 4; i++)
               // {
                //    Playdog[i].MyPicturebox = Dogbox[i];
               // }
    
                for (int i = 0; i < 4; i++)
                {
                    Random randomizer = new Random();
                    Point p = Playdog[i].MyPicturebox.Location;
                    p.X += randomizer.Next(1, 4);
                    Playdog[i].MyPicturebox.Location = p;
                }
                
            }
        }
    }

    2016年4月9日 14:11
  • Hi Mirage_j,

    我推测你出错可能和构造函数那边有关,在未完成界面初始化前你就调用了界面上的控件(未初始化前应该只是一些存放了null值的变量),你可以尝试把那段代码放到 InitializeComponent()后面。

    范例:

            public Form1()
            {
                InitializeComponent();   
                //滞后到控件初始化完成后赋值    
                Playdog = new Greyhound[4];
                Dogbox = new PictureBox[4];
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i] = new Greyhound();
                    Playdog[i].MyPicturebox = new PictureBox();
                    Dogbox[i] = new PictureBox();
                }
                Dogbox[0] = DogBox1;
                Dogbox[1] = DogBox2;
                Dogbox[2] = DogBox3;
                Dogbox[3] = DogBox4;
                for (int i = 0; i < 4; i++)
                {
                    Playdog[i].MyPicturebox = Dogbox[i];
                }
            }

    Regards,

    Moonlight


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    2016年4月18日 6:40