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 reynoldsn, Today, 02:34 PM
          0 responses
          6 views
          0 likes
          Last Post reynoldsn  
          Started by nightstalker, Today, 02:05 PM
          0 responses
          9 views
          0 likes
          Last Post nightstalker  
          Started by llanqui, Yesterday, 09:59 AM
          8 responses
          28 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by quicksandatl, Today, 01:39 PM
          1 response
          6 views
          0 likes
          Last Post quicksandatl  
          Started by md4866, 05-01-2024, 08:15 PM
          2 responses
          18 views
          0 likes
          Last Post md4866
          by md4866
           
          Working...
          X