Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object reference not set to an instance...

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

    Object reference not set to an instance...

    I'm getting Object reference not set to an instance of an object errors in my code. I'm just now trying to understand advanced order handling, so maybe I've done something wrong. Any chance I can send this to support and you could take a look?

    #2
    trend747, this error occurs mainly when you try to access an object that doesn't exist, or when you try to access an object's properties while that object is null (which means the properties don't exist). You can pepper your script with Print() statements to see exactly where the error is occurring and once you've narrowed it down we can help out.

    Code:
    Print("placing order")
    longEnter1 = EnterLong(...);
    Print("order placed, checking some order property");
    if (longEnter1.Name == "some name")
    {
       Print("name matches some name, thus the object reference works fine here and the name matches");
       // do something
    }
    Print("trying something else next");
    The idea is when your code is running and then throws this error, you can see exactly where it stopped by the last printed statement.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Also, please take a look at the reference sample that shows how to work with using OnExecution() and OnOrderUpdate() to set protective orders - http://www.ninjatrader.com/support/f...ead.php?t=7499, as well as the help guide page for IOrders - http://www.ninjatrader.com/support/h...nt7/iorder.htm.
      AustinNinjaTrader Customer Service

      Comment


        #4
        Alright, that helps. Yeah, I was just being lazy... was hoping someone could take a quick look at it... no worries, I'll debug it...

        Comment


          #5
          I've narrowed it down but I'm still perplexed...

          Code:
          private IOrder shortStopOrder = null;
          Code:
          protected override void OnBarUpdate()
                  {           
                      if (Position.MarketPosition == MarketPosition.Flat && shortStopOrder == null)
                      {
                          SetStopLoss(CalculationMode.Ticks, SL);
                          
                          if (Close[1] > Close[2])
                          {
                              Print("Placing short order...");
                              shortStopOrder = EnterShortStop(positionSize, Low[1] - 10 * TickSize, "mrtickles");
                              if (shortStopOrder.Name == "mrtickles")
                              {
                                  Print("It works!");
                              }
                          }
                          else if (High[0] > High[1])
                          {
                              CancelOrder(shortStopOrder);
                          }
                      }
          }
          Note that when I click on Enable for my strategy, it instantly gives me that error. It doesn't even get to any of the Print statements. If I comment out the order section, it works... so it's something to do with the above code.

          The error says Error on calling "OnBarUpdate" method... so as soon as it tries to call it, it errors out...
          Last edited by trend747; 07-15-2011, 10:10 AM.

          Comment


            #6
            trend747, sounds like I'll need to take a look. Please attach either the full strategy, or a stripped down version that still shows this error to your next post. Thanks.
            AustinNinjaTrader Customer Service

            Comment


              #7
              Here's a stripped down version that still errors out.
              Attached Files

              Comment


                #8
                Regardless, you are still missing your initial bars escape statement. You need a:

                Code:
                if (CurrentBar < 2) return;
                statement at the beginning of your OnBarUpdate() method, or it means that, at the start, you are trying to access bars that do not yet exist.

                Comment


                  #9
                  trend747, I strongly believe it is this line causing the error
                  Code:
                  else if (High[0] > High[1])
                  {
                      CancelOrder(shortStopOrder);
                  }
                  because it is executed only if shortStopOrder is null (and the other conditions are true), and you can't cancel a null order.
                  AustinNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Austin View Post
                    trend747, I strongly believe it is this line causing the error
                    Code:
                    else if (High[0] > High[1])
                    {
                        CancelOrder(shortStopOrder);
                    }
                    because it is executed only if shortStopOrder is null (and the other conditions are true), and you can't cancel a null order.
                    You know what, you're right, because we're calling it in a MarketPosition.Flat && shortStopOrder = null situation... you can't be flat in that case.

                    So I'll move that over to my exit code...

                    Thanks a lot man, appreciate it!

                    Comment


                      #11
                      I know what I wanted to do with that offending line... was on a certain condition, cancel the pending stop order... so how do I do that if I can't call it when it's pending and in a market flat state?

                      Comment


                        #12
                        trend, it is really up to you how you want to cancel it. You could do something like this:
                        Code:
                        if (shortStopOrder != null && (shortStopOrder.OrderState == OrderState.Working || shortStopOrder.OrderState == OrderState.Accepted) && cancelConditions == true)
                        {
                           CancelOrder(shortStopOrder);
                        }
                        There are many different things you need to keep in mind when doing IOrders, like all of the possible order states, when to cancel the orders, when to reset the orders to null, etc.

                        The reference sample I linked to earlier should go over most of these concepts.
                        AustinNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        671 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        379 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        111 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        575 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        582 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X