Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Checkbox use giving error??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Checkbox use giving error??

    Ok this has been driving me crazy trying to debug this so I am just going to ask for help. I am trying to figure out how to use a checkbox in my add on. Here is all the pertinent code:

    private CheckBox checkBox;

    (under loadxaml)
    checkBox = LogicalTreeHelper.FindLogicalNode(pageContent, "checkbox") as CheckBox;
    if (checkBox != null)
    checkBox.Checked += CheckBox_Checked;
    checkBox.Unchecked += CheckBox_Unchecked;​

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
    CheckBox checkBox = sender as CheckBox;
    if (checkBox != null)
    {
    outputBox6.Text = "checked";
    }
    }
    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
    CheckBox checkBox = sender as CheckBox;

    if (checkBox != null)
    {
    outputBox6.Text = "unchecked";
    }
    }​

    in the XAML file I have
    <CheckBox x:Name="checkBox" Content="Check Box" IsChecked="False" IsThreeState="False" VerticalAlignment="Top" Margin="36,61,52,0" Grid.Row="1" />

    It will compile fine but whenever I try to open the add on I get "Unhandled exception: object reference not set to an instance of an object"

    If comment out this section:
    //if (checkBox != null)
    // checkBox.Checked += CheckBox_Checked;
    // checkBox.Unchecked += CheckBox_Unchecked;​

    then it loads fine. Can see the checkbox, can check and uncheck it but obviously nothing happens.

    What am I getting wrong??
    -R

    #2
    Hello RaygunWizzle,

    If you are getting an object reference not set error that would indicate that some portion of your code is having a problem. That error means something was null. Its possible that its this part of your code:

    Code:
    if (checkBox != null)
    checkBox.Checked += CheckBox_Checked;
    checkBox.Unchecked += CheckBox_Unchecked;​​
    The if statement is only checking that the first line of code is not null, the second line is not include in that if statement because there are no curly braces around it, you can try this instead:

    Code:
    if (checkBox != null)
    {
    checkBox.Checked += CheckBox_Checked;
    checkBox.Unchecked += CheckBox_Unchecked;​
    }
    If that makes the error go away that means the variable checkBox was null and is why the events are not working.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ah, that did the trick. It will compile and load now. But checking and unchecking the box still does nothing. Here is my XAML code for the box:

      <CheckBox x:Name="checkBox" Content="Check Box" IsChecked="False" IsThreeState="False" VerticalAlignment="Top" Margin="36,61,52,0" Grid.Row="1" />

      and here is the code behind

      private void CheckBox_Checked(object sender, RoutedEventArgs e)
      {
      CheckBox checkBox = sender as CheckBox;
      if (checkBox != null)
      {
      outputBox6.Text = "checked";
      }
      }
      private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
      {
      CheckBox checkBox = sender as CheckBox;

      if (checkBox != null)
      {
      outputBox6.Text = "unchecked";
      }
      }​
      What am I getting wrong? I am assuming its loading now because it is returning Null on the checkbox. But its there in the xaml, and I have it located in the XAML loading code.

      Comment


        #4
        Hello RaygunWizzle,

        That means the control is null or not being found, the error object reference not set means you tried to use something that was null. If correcting the if statement resolves the error that means the control is null and you are not subscribing to any events because no checkbox was found. It looks like you did not use the correct case for the name when trying to find the control, you need to make sure the controls name matches what you are searching for in the .cs code.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Oh SOB I found it.. I forgot to capitalize the B here: checkBox = LogicalTreeHelper.FindLogicalNode(pageContent, "checkbox") as CheckBox;
          Thanks!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by AaronKoRn, Today, 09:49 PM
          0 responses
          11 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Today, 08:42 PM
          0 responses
          10 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Today, 07:51 PM
          0 responses
          11 views
          0 likes
          Last Post strategist007  
          Started by StockTrader88, 03-06-2021, 08:58 AM
          44 responses
          3,980 views
          3 likes
          Last Post jhudas88  
          Started by rbeckmann05, Today, 06:48 PM
          0 responses
          9 views
          0 likes
          Last Post rbeckmann05  
          Working...
          X