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

    I have an indicator, Nick201, that compiles and works perfectly when run as an indicator.

    In my Strategy, I called it using the following code:

    Code:
    private Nick201     nicky;
    
    protected override void OnStartUp()
    {[INDENT]if (nicky == null)[INDENT]nicky = Nick201(1,161000,6,LogicTypes.HATick,2,93000,TimeFrames.IntraDay);
    [/INDENT][/INDENT]}
    Here's where it gets a bit wonky. When I enable the Strategy, the following error message pops up:

    Error on calling 'OnBarUpdate' method for indicator 'Nick201' on bar 104: Object reference not set to an instance of an object.

    I've managed to isolate the error to this offending block of code in Nick201 indicator (NOT the strategy):

    Code:
    private IText     textObject;
    
    protected override void OnBarUpdate()
    {[INDENT]........ 
    
    IdentifyTSP();
    
    [/INDENT][INDENT]..........
    [/INDENT]}
    
    void IdentifyTSP()
    {[INDENT]..........
    textObject = (IText) DrawObjects["5615obj"];
    textObject.TextColor = Color.White;
    ..........
    [/INDENT]}
    Commenting out the textObject block of code allows the Strategy to run.

    I'm a little perplexed as to why the indicator would run without a hitch (and redraws nicely), yet the strategy calling the indicator kicks out a reference error. Any help would be appreciated.

    Thanks!

    #2
    Hello nicbizz,

    If your strategy attempts to access objects that are not available, then you will get that message.

    What is purpose of this line? Your 5615Obj?
    textObject = (IText) DrawObjects["5615obj"];
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hello nicbizz,

      If your strategy attempts to access objects that are not available, then you will get that message.

      What is purpose of this line? Your 5615Obj?
      textObject = (IText) DrawObjects["5615obj"];
      The IText variable, textObject, is to redraw an existing DrawText object, in this case by changing the text color to Color.White. "5615obj" is the tag for DrawText object - I just used a random string as an example.

      The thing is, the above syntax works perfectly when run as an indicator - I can confirm that the correct DrawText object(s) exists each time the code is called, and on the chart, its repainted to white as planned. Its only when I attempt to call the Indicator through a Strategy that the error kicks out. The indicator on its own does NOT produce that error.

      Comment


        #4
        Does your strategy have a text object with tag 5615obj? If it works in the indicator, but not the strategy, the object probably doesn't exist in strategy context.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Hello Ryan,

          No, it does not. Objects and logic is pretty much entirely contained in the indicator. The strategy is extremely simple, and looks like this:

          Code:
          private Nick201             nicky;
                  private IOrder                longEntryOrder = null;
          
                  protected override void Initialize()
                  {
                      CalculateOnBarClose = true;
                      EntriesPerDirection = 1;
                      EntryHandling = EntryHandling.UniqueEntries; 
                      
                      SetProfitTarget("longEntry1",CalculationMode.Ticks, 12);
                      SetStopLoss("longEntry1",CalculationMode.Ticks, 8, false);
                      
                      ClearOutputWindow();
                  }
          
                  protected override void OnStartUp()
                  {
                      if (nicky == null)
                          nicky = Nick201(1,161000,6,LogicTypes.HATick,2,93000,TimeFrames.IntraDay);
                      
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 2)
                          return;
                  
                      if (nicky.Triggered != 1)
                      {
                          
                          
                          if (nicky.OrderIn == false && nicky.Neutral == true)
                          {
                              
                              if (longEntryOrder != null)
                              {
                                  CancelOrder(longEntryOrder);
                                  longEntryOrder = null;
                              }
                              
                          }
                                          
                          if (nicky.OrderIn == true)
                          {
                              
                              if (longEntryOrder == null)
                                  longEntryOrder = EnterLongStop(0,true,2,nicky.OrderPrice,"longEntry1");
                          
                              
                          }
                      }
                  }
          I can't figure out why calling the indicator in the strategy would case an error with the IText reference when it runs perfectly in the indicator, and has no bearing on the strategy at all.

          Is there a way to workaround this? While the IText reference has no bearing on logic, I would very much like for it to remain in the indicator, which is something I cannot do without causing the Strategy to stop working on the first instance of the IText code.

          Comment


            #6
            nicbizz,

            Could you perhaps send this indicator and strategy to us? If you don't want it publicly available you can send it to [email protected] with ATTN : ADAM in the subject line. We don't publish user indicators and strategies.

            Having this code would help in resolving the issue.
            Adam P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            647 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X