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,

    using this code, I get exception only once. Hitting F5 doesn't produce that exception anymore!?

    Code:
                #region HOY, LOY
                if (Bars.BarsType.IsIntraday && Bars.GetDayBar(1) != null && Bars.IsFirstBarOfSession)
                {
                    HOY = Bars.GetDayBar(1).High;
                    LOY = Bars.GetDayBar(1).Low;
                }
                #endregion
    Click image for larger version

Name:	2020-04-22_1354.png
Views:	393
Size:	37.9 KB
ID:	1095851

    Any idea?

    Many thanks

    Thomas
    Attached Files

    #2
    Even closing the chart. Then loading the template, that loads the indicator, that throws exception, doesn't reproduce the error.

    I can reproduce it, if I delete the cached Nymex data series and re-start NT.

    Click image for larger version

Name:	2020-04-22_1404.png
Views:	490
Size:	47.7 KB
ID:	1095854

    Comment


      #3
      td_910... looks like you have captured the exception in Visual Studio... you should be able to see which object has the null reference in VS.

      Do you have more than one dataseries? If so, you might need to select the right one?

      I have a similar situation... am trying to track down an intermittent problem in an indicator...
      Hope this helps.
      T.

      Comment


        #4
        Hi,
        Thanks for your suggestions. It seems, that "Bars.GetDayBar(1)" threw the exception. I use one data series only.
        I changed the code and that seems to work.

        Code:
                    if (Bars.BarsType.IsIntraday && Bars.IsFirstBarOfSession && IsFirstTickOfBar)
                    {
                        HOY = PriorDayOHLC().PriorHigh[0];
                        LOY = PriorDayOHLC().PriorLow[0];
                    }
        Best regards
        Thomas

        Comment


          #5
          Hello td_910,

          It looks like you resolved this however as you mentioned using GetDayBar I just wanted to post the following link, it is expected that GetDayBar can return null so it is suggested to add a null check for that use:https://ninjatrader.com/support/help...tsub=GetDayBar

          In your situation it looks like you have that for GetDayBar so it could have bee the Bars there as well. In general the objects like this which can be null should have a null check surrounding your code. I would suggest adding a null check for Bars as well.


          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hi Jesse,

            isn't that a null check?

            Best regards

            Thomas Click image for larger version

Name:	2020-04-24_1854.png
Views:	393
Size:	74.9 KB
ID:	1096395

            Comment


              #7
              Hello td_910 ,

              Yes and I had mentioned that in my reply:

              In your situation it looks like you have that for GetDayBar so it could have bee the Bars there as well.
              I can't tell from your image what specifically was null, but it seems that you have now corrected the problem at this point. For consistency I wanted to mention that these objects can be null and that you will need null checks/that is in the help guide for others who view this.

              If you are specifically seeing an error while checking if the GetDayBar(1) is null, you can form a script which demonstrates that and attach it so we can review that situation further with the steps you had used.



              Please let me know if I may be of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Sorry I read over that. Friday
                Thanks
                Thomas

                Comment


                  #9
                  Following this thread as I have a similar problem...

                  Jesse, are you saying that it is possible for the Bars object to be null? In my case, I refer to BarsArray[0]...and I get occasional null ref errors... after the chart has been up for some time, there are HEAPS of bars... and then on a new bar, I get a null ref exception thrown. The line of code has nothing other than a reference to BarsArray[0].GetHigh...

                  So..IF it is possible for Bars/BarsArray[0] to be null on like bar number 3000... it would be good to know! Seems weird, but...

                  I can start a new thread if you like... but seems to be much the same issue as td_910 is/was having.

                  Cheers,
                  T.

                  PS I have now added a try/catch block to my code, but running it late Friday, Visual Studio stopped at one instance... and reported BarsArray was indeed null (on a chart that had been running happily for days...) Am waiting for Monday session to open so I can confirm/debug.

                  Update:
                  Well... here's my code line:
                  Code:
                  next_hi = BarsArray[0].GetHigh(TradeDetail.Bar - bar_offset);
                  NT was throwing (occasionally... mostly NOT..) the dreaded Object reference exception.

                  Turns out, the error was due to the parameter to GetHigh being invalid. I would have expected this to cause an index error ... not an Object reference error... but, that's what I got !

                  Sorry for sort of high-jacking this thread... I thought it was a similar issue. Seems not...

                  Regards,
                  Last edited by tgn55; 04-27-2020, 05:37 AM. Reason: Update..

                  Comment


                    #10
                    Hello tgn55,

                    Yes that is certainly possible for the Bars to be null however that is generally only going to be from other contexts, OnBarUpdate should have a bars object as noted in the help guide


                    If you are seeing the GetHigh throwing an error we would likely need to see a specific sample of the structure used there and steps to see that happen. It is possible this relates to the script and its design or we may need to report that as well. As far as I am aware as long as your main bars object is not null you should be able to access the sub methods like GetHigh so long as you are actually in a valid working context which it sounds like you are. The index would be the only other item there that needs checked to make sure thats valid, as long as its being passed a valid index I wouldn't expect any type of exception.



                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by wuannetraam, Today, 02:40 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post wuannetraam  
                    Started by cyberpete76, 03-27-2023, 12:29 AM
                    7 responses
                    268 views
                    1 like
                    Last Post slightly  
                    Started by renewsaltwater, Today, 01:15 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post renewsaltwater  
                    Started by slightly, Today, 12:49 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post slightly  
                    Started by sdauteuil, 09-23-2021, 10:16 AM
                    4 responses
                    1,211 views
                    0 likes
                    Last Post jacobpescaia44  
                    Working...
                    X