Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnRenderTargetChanged error when loading template

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

    OnRenderTargetChanged error when loading template

    I have modified a copy of the ZigZag indicator to draw text and lines at inflection points. It works without error when opening a workspace that contains a relevant chart. If I then load a template to the chart that contains the indicator, OnRenderTargetChanged throws this error:
    2017-03-30 05:53:51:604|3|16|03/30/17 05:53:51 TROUBLE! gZigZag.OnRenderTargetChanged
    Object reference not set to an instance of an object.
    Source: NinjaTrader.Custom
    StackTrace: at NinjaTrader.NinjaScript.Indicators.gws.gZigZag.OnR enderTargetChanged()
    TargetSite: Void OnRenderTargetChanged()
    ChartBars:
    I have other indicators that also use OnRenderTargetChanged() to manage SharpDX objects and those indicators exhibit the same problem of throwing an error only when loading a template.

    The attached zip file contains the indicator, template, and workspace. To reproduce the error, first connect and then open the Sandbox OnRenderTargetChanged workspace, which contains a 1-Minute chart with the gZigZag indicator. No error should occur.

    Next, load the aZigZagOnRenderTargetChanged template, which should throw the Object reference not set error.

    Another curiosity is that the string following "ChartBars:" seems to be empty. It gets assigned in the State.DataLoaded block and normally contains information about the chart's bars object.
    Attached Files

    #2
    Hello,

    Thank you for posting.

    I reviewed the attached file and do see the error while completing the steps you had.
    Object reference not set to an instance of an object. specifically, this indicates that some object you are using is null while you try to use it. This can occur when the render target changes or other actions on the chart complete. Depending on what objects you are using, you would likely need to add null checks how you currently are. I noted that you are not checking for null on all objects that you use that can be null such as ChartControl and some of the brushes being used. I added the following and can see the OnRenderTarget error disappear but then we get other errors from OnRender.

    Code:
    if(cdxbrush_high!= null && ColorHigh != null)cdxbrush_high	= ColorHigh.ToDxBrush(RenderTarget);
    if(cdxbrush_high!= null && ColorLow != null)cdxbrush_low	= ColorLow.ToDxBrush(RenderTarget);
    if(cdxtextformat!= null && ChartControl != null)cdxtextformat 	= ChartControl.Properties.LabelFont.ToDirectWriteTextFormat()
    ;

    Due to the amount of logic in this sample, this would be outside what our support could provide debugging assistance with. I would instead suggest reducing this further to locate other error locations in the logic and to ensure to check for null objects wherever possible.


    I look forward to being of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      I noted that you are not checking for null on all objects that you use that can be null such as ChartControl and some of the brushes being used. I added the following and can see the OnRenderTarget error disappear but then we get other errors from OnRender.

      Code:
      if(cdxbrush_high!= null && ColorHigh != null)cdxbrush_high    = ColorHigh.ToDxBrush(RenderTarget);
      if(cdxbrush_high!= null && ColorLow != null)cdxbrush_low    = ColorLow.ToDxBrush(RenderTarget);
      if(cdxtextformat!= null && ChartControl != null)cdxtextformat     = ChartControl.Properties.LabelFont.ToDirectWriteTextFormat();
      The culprit was ChartControl, which apparently is null when loading a template. Your code errs in that the SharpDX brushes would never get assigned, since you're only assigning them when they are not null. So, of course when OnRender() attempts to use them it will throw an error.

      The following code fixes the problem by assigning fallback objects in case the desired object is null:
      Code:
      cdxbrush_high = ColorHigh != null ? ColorHigh.ToDxBrush(RenderTarget) : Brushes.DarkRed.ToDxBrush(RenderTarget);
      cdxbrush_low = ColorLow != null ? ColorLow.ToDxBrush(RenderTarget) : Brushes.Green.ToDxBrush(RenderTarget);
      cdxtextformat = ChartControl != null ? ChartControl.Properties.LabelFont.ToDirectWriteTextFormat()
                              : new Gui.Tools.SimpleFont("Consolas", 14).ToDirectWriteTextFormat(); [COLOR=SeaGreen]// just in case ChartControl is null[/COLOR]
      Thanks for pointing me in the right direction. In the future, I shall be diligent in always checking for null.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      42 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      25 views
      0 likes
      Last Post PaulMohn  
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      162 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      98 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      157 views
      2 likes
      Last Post CaptainJack  
      Working...
      X