1

I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.

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.
    }
  }
}
2
  • as is said at the points where is used blockpoint topSP.Children.Add(myWrapPanel); and AddWrapPanelTop(); Commented Mar 28, 2014 at 8:00
  • In wpf do not assume everything on the UI is ready after InitializeComponent(). You should consider not addressing UI controls before the Loaded event handler Commented Mar 28, 2014 at 8:28

1 Answer 1

2

You define the following:

StackPanel topSP = null;

Then you have

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

This is in your local scope though so will be destroyed when you exit the function

Finally you have

topSP.Children.Add(myWrapPanel);

This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.

In order to resolve simply change the second definition to:

topSP = new StackPanel { Width = 400, Height = 50 };
Sign up to request clarification or add additional context in comments.

4 Comments

thanks that was the problem just that he throws now a 3-9 expetion
What is a 3-9 exception?
i found the next problem, WrapPanel wp = (WrapPanel)topSP.Children[0]as WrapPanel; wp.Children.Add(txtB1); is there a way to cast it liek that WrapPanel)topSP.Children[0]as WrapPanel.Add(txtB1)
a 3-9 problem is when there error is in the line 3 and 9 whish is always the premade xaml code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.