日期:2014-05-17  浏览次数:20718 次

请高手们帮我看下这段代码!加粗这句代码系统提示未将对象引用设置到对象的实例
请高手们帮我看下这段代码!加粗这句代码系统提示未将对象引用设置到对象的实例。 
 private static Panel Var_Panel = new Panel();
  private static PictureBox Var_Pict = new PictureBox();
  private static int Var_i = 0;
  private Font Var_Font = new Font("宋体", 9);

  private void pictureBox_1_Click(object sender, EventArgs e)
  {
  Var_i = Convert.ToInt16(((PictureBox)sender).Tag.ToString());
  switch (Var_i)
  {
  case 1:
  {
  Var_Panel = panel_Gut_1;
  Var_Pict = pictureBox_1;
  break;
  }
  case 2:
  {
  Var_Panel = panel_Gut_2;
  Var_Pict = pictureBox_2;
  break;
  }
  case 3:
  {
  Var_Panel = panel_Gut_3;
  Var_Pict = pictureBox_3;
  break;
  }

  }
  if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 0 || Convert.ToInt16(Var_Panel.Tag.ToString()) == 2) {
  Var_Panel.Tag = 1 
  Var_Pict.Image = null;
  Var_Pict.Image = Properties.Resources.down;
  Var_Panel.Visible = false;
  }
  else
  {
  if (Convert.ToInt16(Var_Panel.Tag.ToString()) == 1)
  {
  Var_Panel.Tag = 2
  Var_Pict.Image = null;
  Var_Pict.Image = Properties.Resources.up;
  Var_Panel.Visible = true;
  }
  }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  pictureBox_1.Image = null;
  pictureBox_1.Image = Properties.Resources.up;
  pictureBox_2.Image = null;
  pictureBox_2.Image = Properties.Resources.up;
  pictureBox_3.Image = null;
  pictureBox_3.Image = Properties.Resources.up;
  Var_Font = label_1.Font;
  }

  private void label_1_MouseEnter(object sender, EventArgs e)
  {
  ((Label)sender).ForeColor = Color.Gray;
  ((Label)sender).Font = new Font(Var_Font, Var_Font.Style | FontStyle.Underline);
  }

  private void label_1_MouseLeave(object sender, EventArgs e)
  {
  ((Label)sender).ForeColor = Color.Black;
  ((Label)sender).Font = new Font(Var_Font, Var_Font.Style);
  }
  }

------解决方案--------------------
是NullReferenceException吧,那个Tag需要先设定好才能用的,否则是缺省值null,用的时候自然就出现这个异常了。

打断点检查一下。