意外的NullReferenceException / InvocationTargetException

时间:2014-03-28 07:53:56

标签: c# wpf nullreferenceexception invocationtargetexception

我有一个指向null错误,但我没有看到问题所在,因为一切都在使用之前被初始化。错误的点我已经给出了一个块引用。总是感谢每一个帮助。

Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;      

public MainWindow()
{
      InitializeComponent();

      loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
      Canvas.SetRight(loadBut, 160);
      Canvas.SetBottom(loadBut, 24);
      canvas1.Children.Add(loadBut);

      saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
      Canvas.SetRight(saveBut, 80);
      Canvas.SetBottom(saveBut, 24);
      canvas1.Children.Add(saveBut);

      StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
      Canvas.SetLeft(topSP, 160);
      Canvas.SetTop(topSP, 100);

      AddWrapPanelTop();

      AddTextBoxTop();
      AddTopButton();
}

void AddTextBoxTop()
{
     TextBox txtB1 = new TextBox();
     txtB1.Text = "Text";
     txtB1.Width = 75;
     txtB1.Height = 75;
     WrapPanel wp = (WrapPanel)topSP.Children[0];
     wp.Children.Add(txtB1);   
 }

 void AddWrapPanelTop()
 {    
      WrapPanel myWrapPanel = new WrapPanel();

      SolidColorBrush mySolidColorBrush = new SolidColorBrush();
      mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);

      myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
      myWrapPanel.Orientation = Orientation.Horizontal;
      myWrapPanel.Width = 4000;
      myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
      myWrapPanel.VerticalAlignment = VerticalAlignment.Top;

      topSP.Children.Add(myWrapPanel);
  }

  void AddTopButton()
  {    
       TextBox txtB1 = new TextBox();
       txtB1.Background = System.Windows.Media.Brushes.Magenta;
       txtB1.Text = "Text";
       txtB1.Width = 75;
       txtB1.Height = 75;
       topAddBut = new Button();
       topAddBut.Click += new RoutedEventHandler(this.TopClick);
       topAddBut.Content = "Add";
       topAddBut.Width = 75;

       // Add the buttons to the parent WrapPanel using the Children.Add method.
       WrapPanel wp = (WrapPanel)topSP.Children[0];
       wp.Children.Add(txtB1);
       wp.Children.Add(loadBut);
       this.topSP.Children.Add(wp);
   }

   void TopClick(Object sender, EventArgs e)
   {
        TextBox txtB1 = new TextBox();
        txtB1.Background = System.Windows.Media.Brushes.Magenta;
        txtB1.Text = "Text";
        txtB1.Width = 75;
        txtB1.Height = 75;
        Button s = (Button)sender;
        WrapPanel wp = (WrapPanel)s.Parent;
        wp.Children.Remove(s);
        wp.Children.Add(txtB1);
        AddTopButton();

        // Add the buttons to the parent WrapPanel using the Children.Add method.
    }
  }
}

1 个答案:

答案 0 :(得分:2)

您定义以下内容:

StackPanel topSP = null;

然后你有

StackPanel topSP = new StackPanel { Width = 400, Height = 50 };

这是在你的本地范围内,但是当你退出函数

时会被销毁

最后你有

topSP.Children.Add(myWrapPanel);

这仍将设置为null,这就是您的错误发生的原因。基本上,您正在创建同一变量的本地版本。

为了解决,只需将第二个定义更改为:

topSP = new StackPanel { Width = 400, Height = 50 };