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

    Hello again with the same question and still don't have the answer.

    I have simplified the code the maximum so maybe someone can troubleshoot it.

    I am still getting the error Object reference not set to an instance of an object. I understand this is has to do with the init of the order and more precisely the Name of the order.

    any comment will help.



    Code:
     
    
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
     public class testordermanagement : Strategy
     {
      private Order entryOrder = null; // This variable holds an object representing our entry order
    
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
        Description         = @"Enter the description for your new custom Strategy here.";
        Name          = "testordermanagement";
        Calculate         = Calculate.OnBarClose;
        EntriesPerDirection       = 1;
        EntryHandling        = EntryHandling.AllEntries;
        IsExitOnSessionCloseStrategy    = true;
        ExitOnSessionCloseSeconds     = 30;
        IsFillLimitOnTouch       = false;
        MaximumBarsLookBack       = MaximumBarsLookBack.TwoHundredFiftySix;
        OrderFillResolution       = OrderFillResolution.Standard;
        Slippage         = 0;
        StartBehavior        = StartBehavior.WaitUntilFlat;
        TimeInForce         = TimeInForce.Gtc;
        TraceOrders         = false;
        RealtimeErrorHandling      = RealtimeErrorHandling.StopCancelClose;
        StopTargetHandling       = StopTargetHandling.PerEntryExecution;
        BarsRequiredToTrade       = 20;
        // Disable this property for performance gains in Strategy Analyzer optimizations
        // See the Help Guide for additional information
        IsInstantiatedOnEachOptimizationIteration = true;
       }
       else if (State == State.Configure)
       {
        if (entryOrder != null)
                        entryOrder = GetRealtimeOrder(entryOrder);
    
       }
      }
    
      protected override void OnBarUpdate()
      {
    
       if (CurrentBar < BarsRequiredToTrade)
        return;
    
    
    
       if ( Close[0] > Open[0])
       {
        EnterLongLimit(0, true, 1, Close[0] , "LongEntry");
       }
    
    
       if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
        {
         CancelOrder(entryOrder);
        }
    
      }
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
    
                if (order.Name == "LongEntry")
                {
                    entryOrder = order;
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                    {
                        entryOrder = null;
                    }
                }
            }
    
     protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
    
                if (entryOrder != null && entryOrder == execution.Order)
                {
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            entryOrder = null;
                        }
    
                }
    
               
            }
     }
    }

    #2
    Code:
     
     if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
    should be

    Code:
     
     if (entryOrder != null && entryOrder.Name == "LongEntry" && Close[0] < Open[0])
    Maybe there are other issues, but this one was easy to identify

    When looking for an error, one helpful method is to surround your code with a try/catch block, if the error gets caught have the block cover fewer lines and try again.

    E.g.:
    Code:
        protected override void OnBarUpdate()
        {
            try
            {
                if (CurrentBar < BarsRequiredToTrade)
                   return;
    
    
    
                if ( Close[0] > Open[0])
                {
                    EnterLongLimit(0, true, 1, Close[0] , "LongEntry");
                }
    
    
                if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
                {
                    CancelOrder(entryOrder);
                }
            }
            catch(Exception ex)
            {
                Print(ex.StackTrace);
            }
        }
    Unfortunately no line numbers are shown in the stack trace.

    Comment


      #3
      Hello madams212121,

      Thanks for your post.

      From observance, I can say that GetRealtimeOrder should be used in State.Realtime and not in State.Configure, but I do not think this is the source of your troubles.

      You will need to take debugging steps to see which line of code is throwing the error.

      The log tab of the Control Center will tell you which method the error came from.

      You can then add prints within that method to identify which line of code is throwing the error. I recommend using the Playback Connection to reproduce behavior that happened with realtime processing so you can repeat the occurrence and take debugging steps.

      MojoJojo's point that checking entryOrder's name without first checking if entryOrder is null could hit this error. Adding try catches as he mentions would be another way to identify which part of your code is throwing the error.

      Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

      Using try/catches - https://ninjatrader.com/support/help...tch_blocks.htm

      The SampleOnOrderUpdate strategy can be used as a reference for using GetRealtimeOrder, Order objects, and the Advanced Order Handling methods.



      Playback Connection - https://ninjatrader.com/support/help.../?playback.htm

      Please let us know if we can be of further assistance.

      Comment


        #4
        ok thank you. Yes checking for the Not null help with the code

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X