Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object reference not set to an instance of an object

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

    Object reference not set to an instance of an object

    Need some help here. Getting the error "Object reference not set to an instance of an object" in the log. This is all I get in the output window...all of which printed between 83000 and 83030:

    WITHIN ONBARUPDATE
    WITHIN ONBARUPDATE
    WITHIN BARSINPROGRESS = 1 SECTION

    The OnBarUpdate section then stops running and I get the error message. I'm applying this on a 1 min chart and adding a secondary bars set to tick by tick.

    [code]
    protectedoverridevoid Initialize()
    {
    Add(PeriodType.Tick, 1);
    Add(SMA(Period));
    Add(BarTimer());
    TraceOrders = true;
    CalculateOnBarClose = true;
    }
    protectedoverridevoid OnBarUpdate()
    {
    Print("WITHIN ONBARUPDATE");
    if (BarsInProgress == 0)
    return;
    if (BarsInProgress == 1)
    {
    Print("WITHIN BARSINPROGRESS = 1 SECTION");
    if (ToTime(Time[0]) >= Mar****pen && ToTime(Time[0]) < MarketClose)
    {
    Print("WITHIN MARKET HOURS SECTION");
    if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat)
    {
    //Buy if current tick is above SMA of primary bars previous bar
    Print("WITHIN ENTRY CONDITIONS SECTION");
    if (Close[0] > SMA(BarsArray[0], Period)[1])
    {
    myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy");
    SetStopLoss("Buy",CalculationMode.Ticks, 10, false);
    barNumberOfOrder = CurrentBar;
    Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION");
    }
    }
    elseif (myEntryOrder != null && Position.MarketPosition == MarketPosition.Flat)
    {
    Print("WITHIN ENTRY CONDITIONS SECTION 2");
    if (Close[0] > SMA(BarsArray[0], Period)[1])
    {
    myEntryOrder = EnterLongLimit(1, true, 100, Closes[0][1], "Buy");
    SetStopLoss("Buy",CalculationMode.Ticks, 10, false);
    barNumberOfOrder = CurrentBar;
    Print("WITHIN ENTRY CONDITIONS SATISFIED SECTION 2");
    }
    }

    }
    }

    if (CurrentBar > barNumberOfOrder + 2)
    CancelOrder(myEntryOrder);
    }

    protectedoverridevoid OnOrderUpdate(IOrder order)
    {
    if (myEntryOrder != null && myEntryOrder.Token == order.Token)
    {
    Print(order.ToString());
    if (order.OrderState == OrderState.Filled)
    myEntryOrder = null;
    }
    }

    #2
    Hello,

    Try commenting out lines of code and conditions until it progresses, then you will know what is the cause. You might want to start by commenting out this line since the Print() right above it was the last thing to work:
    if (ToTime(Time[0]) >= Mar****pen && ToTime(Time[0]) < MarketClose)
    DenNinjaTrader Customer Service

    Comment


      #3
      still not working

      Originally posted by NinjaTrader_Ben View Post
      Hello,

      Try commenting out lines of code and conditions until it progresses, then you will know what is the cause. You might want to start by commenting out this line since the Print() right above it was the last thing to work:
      if (ToTime(Time[0]) >= Mar****pen && ToTime(Time[0]) < MarketClose)
      I commented out the time filter code as you suggested, and it did place one order but then once that position was closed out, OnBarUpdate appeared to stop running again. Not sure why the time filter would have held it up in the first place because the Market Replay I was running was within an appropriate time frame (although I was testing outside of the timeframe).

      I've attached the strategy. If someone could try it and see if they get the same results that would be helpful. First time trying to do a Multi-time frame strategy, so I'm sure I'm just missing something. Although, most of the code in the attached strategy was copied right out of NT reference samples. Thanks,

      kc
      Attached Files

      Comment


        #4
        Hello,

        Print() your Mar****pen and MarketClose and your ToTime() values just prior to the condition and see what values are being used.
        DenNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ben View Post
          Hello,

          Print() your Mar****pen and MarketClose and your ToTime() values just prior to the condition and see what values are being used.
          That is not the issue. I ran it this morning with real market data and it gets past that. It prints everything up to the line of code listed below, and then OnBarUpdate throws the error "Object reference not set to an instance of an object" in the Log.
          Code:
           
          if (myEntryOrder == null && Position.MarketPosition == MarketPosition.Flat)

          Does it have something to do with == null?

          Or perhaps something in the code under that line where I'm asking for information about a certain bar and I don't have that many bars yet or something?

          Thanks,

          kc

          Comment


            #6
            Here is likely your problem.

            if (CurrentBar > barNumberOfOrder + 2)
            CancelOrder(myEntryOrder);

            You can't call cancel order on a null IOrder object. You need to check for null before.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              Here is likely your problem.

              if (CurrentBar > barNumberOfOrder + 2)
              CancelOrder(myEntryOrder);

              You can't call cancel order on a null IOrder object. You need to check for null before.

              That was it. I have my real strategy working now. As usual, thanks for the great support.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              558 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              324 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              545 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              547 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X