2014-03-28 67 views
1

我有一個指向空錯誤,但我沒有看到問題出在哪裏,因爲在使用之前,所有東西都被初始化了。錯誤所在的點我給出了一個blockquote。就像我總是很感謝每一個幫助。意外的NullReferenceException/InvocationTargetException

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. 
    } 
    } 
} 
+0

如在其中使用blockpoint topSP.Children.Add(myWrapPanel)點是說;和AddWrapPanelTop(); –

+0

在wpf中,不要假設用戶界面上的所有內容都在'InitializeComponent()'後面就緒。你應該考慮在'Loaded'事件處理程序 – Silvermind

回答

2

可以定義如下:

StackPanel topSP = null; 

然後,你必須

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

這是你的局部範圍內,但這樣會在退出功能

最後被銷燬你有

topSP.Children.Add(myWrapPanel); 

這仍然會設置爲null,這就是您的錯誤發生的原因。基本上你正在創建一個相同變量的本地版本。

爲了解決簡單地改變所述第二定義:

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

之前不要處理UI控件,這是因爲他現在拋出3-9期待的問題 –

+0

什麼是3-9異常? – Andrew

+0

我發現下一個問題,WrapPanel wp =(WrapPanel)topSP.Children [0]作爲WrapPanel; wp.​​Children.Add(txtB1);是否有一種方法來欺騙WrapPanel)topSP.Children [0]作爲WrapPanel.Add(txtB1) –