Announcement

Collapse
No announcement yet.

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.

    Hello,

    I'm currently developing an indicator and when I load the indicator it only runs up to bar 4 but then I get this error: "Error on calling 'OnBarUpdate' method on bar 4: Object reference not set to an instance of an object." By the way, it's calculating OnEachTick. Below is the code I'm writing:

    Code:
    public class Prueba : Indicator
        {      
            private Series<List<double>> listPrices;
        }​
    Code:
    else if (State == State.Configure)
                {
                    ClearOutputWindow();
                    AddVolumetric(null, BarsArray[0].BarsPeriod.BarsPeriodType, BarsArray[0].BarsPeriod.Value, VolumetricDeltaType.BidAsk, 1);
                }
                else if (State == State.DataLoaded)
                {
                    listPrices        = new Series<List<double>> (this, MaximumBarsLookBack.Infinite);​
                }
    Code:
    protected override void OnBarUpdate()
            {            
                if (BarsInProgress == 1)
                {
                    if (Bars == null)
                    return;
                    
                    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType barsType = BarsArray[1].BarsType as
                        NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsType;
                    
                    if (barsType == null)
                        return;
                    
                    double prices = Close[0];
                    
                    if(IsFirstTickOfBar)
                    {
                        // create the list once per bar
                        listPrices[0] = new List<double>();
                    }
                    
                    listPrices[0].Add(prices);
                    
                    foreach(double price in listPrices[0])
                    {
                        Print("Current Bar: " + CurrentBar + "       " + price);
                    }​


    The same thing happens to me if I use a dictionary instead of a list.

    Could you please help me with this problem?​​

    #2
    Hello DanielRMejia,

    Using variables dynamically with AddDataSeries() methods, including AddVolumetric(), is not supported.

    "Warning
    Arguments supplied to AddVolumetric() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    Using BarsArray[0].BarsPeriod.BarsPeriodType in AddVolumetric() is not supported and will break the Strategy Analyzer.


    Your series listPrices is synchronized with the primary series and has bar updates with BarsInProgress 0.

    You could choose to assign a new object in BarsInProgress 0.

    You could choose to synchronize the series with the added series BarsArray[1].

    listPrices = new Series<List<double>> (BarsArray[1], MaximumBarsLookBack.Infinite);​

    Further, you should check that the list is not null before attempting to call .Add().

    if (listPrices != null && listPrices.Count > 1)
    {
    listPrices[0].Add(prices);
    }
    else
    {
    Print("{0} | BarsInProgress: {1}, listPrices is null or Count is 0", Time[0], BarsInProgress));
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      The code is working well with your solution.

      Thank you Chelsea B. for your help.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      556 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X