Announcement

Collapse
No announcement yet.

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.

        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 Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            596 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            554 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X