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

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

    Hi,

    I got this error message "Error on calling 'OnBarUpdate' method on bar 12: Object reference not set to an instance of an object."

    What does this actually mean?

    Here's the code:-
    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBar < (level*2 + 10)) return;
    
    // Identify the first pivot low
    if((Low[1] < Low[0]) && (Low[1] < Low[2]))
    {
    DemandPoint0 = Low[1];
    DemandPoint0Y = 1;
    Draw.Dot(this, "DPDot", true, DemandPoint0Y, DemandPoint0, Brushes.Red, true);
    
    }
    
    // Hunting for a lower pivot low that has already occurred which meets the same pivot low condition and to draw a ray connecting the 2 points
    for( int x = 2; x <= 100; x++)
    {
    if ((Low[x] < Low[x-1]) && (Low[x] < Low[x+1]))
    {
    if (Low[x] < DemandPoint0)
    {
    Draw.Ray(this, "DPL1", true, x, Low[x], DemandPoint0Y, DemandPoint0, Brushes.Pink, DashStyleHelper.Solid, 1);
    }
    }
    }
    }
    Regards
    Kay Wai
    Last edited by kaywai; 05-22-2022, 12:51 PM.

    #2
    In most cases, that error means you have a logic error in your code.
    Somewhere, somehow, your code is trying to access something
    that is not defined.

    Unfortunately, 'not defined' can mean most anything -- in your case,
    it looks like you're trying to access Low[x] where the 'x' value is
    simply too large. For Series variables, such as High, Low, etc,
    the index value 'x' is known as the 'BarsAgo' index, and it should
    never be greater than the current value of CurrentBar.

    -=o=-

    What is the initial value of your 'level' variable?
    You don't show that. I'm guessing it is '1'.

    Why '1'?
    Because on the 12th bar the error occurs.

    Where is the error?
    Inside your 'for' loop. The loop wants to look back 100 bars to find
    a previous pivot low, I presume.

    The logic error in your code is right there.
    Why? Because when there are only 12 bars on the chart, you can't
    ask the code to access the 13th bar ago -- doing so results in the
    error you're getting.

    There is nothing wrong with looking back 100 bars to do what you
    want to do -- but it is up to you to add guard code to make sure
    enough bars exist to prevent trying to access non-existent bars.

    That is, when CurrentBar is 12, accessing Low[x] where x > 12
    means you're trying to access a non-existent bar -- and you'll get
    an error.

    How to fix it?

    Try changing this line,
    for (int x = 2; x <= 100; x++)

    to something like this,
    for (int x = 2; x <= Math.Max(CurrentBar-1, 100); x++)

    That Math.Max() is your 'guard code', making sure enough bars exist, so
    that accessing Low[x] or Low[x-1] or Low[x+1] doesn't fail.

    Make sense?
    Last edited by bltdavid; 05-23-2022, 12:42 AM.

    Comment


      #3
      Hello kaywai,

      The error 'Object reference not set to an instance of an object' means that you are using a variable with no object assigned to it (a null value).
      An object reference is a variable. 'Not set to an instance of an object' means nothing was assigned to that variable.
      You will need to check for null before using any variable that be null.

      Hello, I keep running into an error of &quot;Object reference not set to an instance of an object.&quot; when I set the entryOrder to Null after it has been cancelled. Below is the block of code that I found in several of the example strategies. protected override void OnOrderUpdate(Order order, double limitPrice,
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        bltdavid thank you very much. that was very helpful and it did the job!

        Comment


          #5
          kaywai You should also consider using BarsRequiredToTrade: https://ninjatrader.com/support/help...redtotrade.htm

          At its simplest, you need only include this as the first line in OnBarUpdate: if (CurrentBar < BarsRequiredToTrade) return;

          Thanks.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Thank you jeronymite. Will take a look.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by frankthearm, Today, 09:08 AM
            7 responses
            29 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by NRITV, Today, 01:15 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by maybeimnotrader, Yesterday, 05:46 PM
            5 responses
            25 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by quantismo, Yesterday, 05:13 PM
            2 responses
            17 views
            0 likes
            Last Post quantismo  
            Started by adeelshahzad, Today, 03:54 AM
            5 responses
            33 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X