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 instance of an object

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

    object reference not set to instance of an object

    Hello,

    I have the following custom method:

    privatebool IsTrough(IDataSeries price, int endBarsAgo)
    {
    if (CurrentBar - endBarsAgo < 2)
    returnfalse;

    if (price[endBarsAgo + 1] < price[endBarsAgo])
    {
    for (int i = 2; i <= CurrentBar - endBarsAgo; i++)
    {
    if (price[endBarsAgo + i] == price[endBarsAgo + 1])
    continue;
    elseif (price[endBarsAgo + i] > price[endBarsAgo + 1])
    returntrue;
    elseif (price[endBarsAgo + i] < price[endBarsAgo + 1])
    returnfalse;
    }
    }

    returnfalse;
    }

    When I run MRO(delegate{return IsTrough(lPrice, 0);}, 1, CurrentBar) sometimes I get the error message in the title of this thread.

    Do you know what am I doing wrong?

    Thanks in advance,

    Stopped

    #2
    "object reference not set to instance of an object" error occurs usually when you're trying to access dataseries which has not yet been initialized

    Comment


      #3
      Thank you BankRobber.

      Does anyone, particularly from NT support, know why I am getting this error on this particular example when using the MRO function while calling my custom method?

      Thanks again beforehand.

      Comment


        #4
        Stopped, I would not see an obivous issue with the code, you would need debug your method / MRO call of it to see where it could run into accessing the null object.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hi Bertrand,

          I have checked the code and it looks as if the error happens when the MRO function is searching for the 'true' condition in the first few (normally 0 to 2) bars, as the IsTrough method needs 3 bars to generate a result.

          Can you think of a line of code I can add to the custom method to deal with 'null' references (i.e. to produce a 'false' rather than an error message)?

          Thank you again,

          Stoppped

          Comment


            #6
            Originally posted by Stopped View Post
            Hi Bertrand,

            I have checked the code and it looks as if the error happens when the MRO function is searching for the 'true' condition in the first few (normally 0 to 2) bars, as the IsTrough method needs 3 bars to generate a result.

            Can you think of a line of code I can add to the custom method to deal with 'null' references (i.e. to produce a 'false' rather than an error message)?

            Thank you again,

            Stoppped
            It does look as if your code needs at least 3 bars. Therefore, you may want to change your first line to read:

            Code:
            if (CurrentBar - endBarsAgo < 3)
            Maybe less elegant, but more robust would be to simply escape the first three bars in OnBarUpdate(), using a sledge hammer to obviate the problem so to speak, as nothing will be called until you have 3 bars on the chart.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by RookieTrader, Today, 09:37 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by kulwinder73, Today, 10:31 AM
            0 responses
            5 views
            0 likes
            Last Post kulwinder73  
            Started by terofs, Yesterday, 04:18 PM
            1 response
            23 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by CommonWhale, Today, 09:55 AM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by Gerik, Today, 09:40 AM
            2 responses
            7 views
            0 likes
            Last Post Gerik
            by Gerik
             
            Working...
            X