Announcement

Collapse
No announcement yet.

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