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

System.NullReferenceException

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

    System.NullReferenceException

    Hi, wondering if you can provide some insight. I have a "for" loop in my OnRender section (see below) that is designed to go back to a certain point (startBarIndex) and retrieve each price from that point forward to check if it has exceeded a certain price level (selectedFirstPrice). If price does exceed the price level, it increments a counter. The loop seems to work for a number of iterations, but then all of a sudden fails with the message below. I'm at a loss as to why it is failing. Furthermore, if I run this code on a 1 minute chart, it works fine, but when I run it on a 5 min chart, it fails. Any comments/suggestions to help fix this would be greatly appreciated. thanks!

    for (int i = (startBarIndex) ; i < CurrentBar; i++) {if (GetValueAt(i) != null && GetValueAt(i) > selectedFirstPrice) hit++;}

    Error message:
    System.NullReferenceException: Object reference not set to an instance of an object.
    at NinjaTrader.Data.BarsSeries.GetClose(Int32 index)
    at NinjaTrader.Data.Bars.GetClose(Int32 index)
    at NinjaTrader.NinjaScript.NinjaScriptBase.GetValueAt (Int32 barIndex)
    at NinjaTrader.NinjaScript.Indicators.MyNT8SupplyDema ndZones.OnRender(ChartControl chartControl, ChartScale chartScale)



    #2
    Hello pman777,

    Its hard to be sure without a demonstration script that can reproduce the issue.

    But it may be an invalid bar index used. What is the count of that series and what is the index used?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Chelsea, I hope I understand what your asking ... in the case above, the startBarIndex = 2860 and CurrentBar = 20685 ... the loop went from 2860 to 4144 (1284 iterations) before failing.

      Comment


        #4
        Hello pman777,

        I recommend you use prints to debug the script.

        Print the index (your variable i in the loop) and print the <Series>.Count.


        Also, you are attempting to use CurrentBar in OnRender().
        CurrentBar is synchronized in the instrument thread not in the render thread.
        You must use TriggerCustomEvent() if you want to use CurrentBar in OnRender() and this will cost performance.

        Below is a public link on TriggerCustomEvent().
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          thanks again for your help Chelsea ... seems TriggerCustomEvent() solved the problem ... still more testing to do but looking good. This has also helped me better understand the processing model for OnRender.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GussJ, 03-04-2020, 03:11 PM
          16 responses
          3,281 views
          0 likes
          Last Post Leafcutter  
          Started by WHICKED, Today, 12:45 PM
          2 responses
          19 views
          0 likes
          Last Post WHICKED
          by WHICKED
           
          Started by Tim-c, Today, 02:10 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Taddypole, Today, 02:47 PM
          0 responses
          5 views
          0 likes
          Last Post Taddypole  
          Started by chbruno, 04-24-2024, 04:10 PM
          4 responses
          53 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Working...
          X