Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsArray check inside OnRender

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

    BarsArray check inside OnRender

    I'm iterating through the primary bars object and I would like to perform some calculations between the primary bars and an additional instrument I added using:
    Code:
    AddDataSeries("XLK", BarsPeriodType.Minute,1);
    How can I check that the secondary BarsArray[1] actually exists at the index before doing something with it? If I don't check I get this error:

    Code:
    Indicator 'CustomRender3': Error on calling 'OnRender' method on bar 1632: Object reference not set to an instance of an object.
    Here is the code:
    Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
       if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBar < 1) {
        return;
       }
       base.OnRender(chartControl, chartScale);
       for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex;idx++){ 
    	   double		    spy					= BarsArray[0].GetHigh(idx);
    	   double		    xlk					= BarsArray[1].GetClose(idx);
    	  
    	   //Printing a meaningless calculation for demonstration;   
    	   Print(xlk + spy);    
       }
      }
    Here is what I am trying to do:

    Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale) {
       if (ChartBars.Bars == null || Bars.Instrument == null || CurrentBar < 1) {
        return;
       }
       base.OnRender(chartControl, chartScale);
       for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex;){ 
    	   double		    spy					= BarsArray[0].GetHigh(idx);
    	   
               if(BarsArray[1] == null){
                 //don't advance the counter. Just return and wait for BarsArray[1] to be an object
                return;
               }else{
                 double		    xlk					= BarsArray[1].GetClose(idx);
    	     Print(xlk + spy); 
                 idx++;
    
               }
       
       }
      }
    I think the error happens because the primary bars object is available slightly before the secondary object is actually available for me to call. I just don't know what the right way would be to check for it.

    #2
    Hello swcooke,

    Generally, you would do as you already are, or check if the object is null. Are you certain BarsArray[1] is what is null?

    Have you checked all of the objects you use that can be null to see what specifically is in control of the error?

    If the primary is null, you would also need to check if that is null in addition to the second series.


    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    637 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    366 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    569 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    571 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X